i have a user model which has one image. how can i disable the autosave option. when i tried the following i am getting an error. Am using rails 3.
class User &开发者_如何学编程lt; ActiveRecord::Base
has_one :image_mapping, :class_name=>"ImageMapping",:as => :imageable
has_one :image, :through => :image_mapping, :class_name => "Image", :autosave => false
end
It says Unknown key(s): autosave
isn't loading autosave_association.rb properly?This probably doesn't solve your problem, but perhaps you find it helpful anyway:
There seems to be a problem with has_one :through
. For example in class Post
,
has_one :owner, :through => :blog
works fine, but
has_one :owner, :through => :blog, :autosave => false
doesn't. If you instantiate a Post
, like so
post = Post.find 1
you get this:
ArgumentError: Unknown key(s): autosave
from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/associations.rb:1758:in `create_has_one_through_reflection'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/associations.rb:1102:in `has_one'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/autosave_association.rb:137:in `has_one'
[...]
Looking at the documentation, I can't find a reason why it shouldn't work. And in fact it seems to work fine with has_many
, only has_one
hiccups if the :autosave
option is set.
Unless somebody here on Stack Overflow finds something wrong with using :has_one
in this way, you could also go to the Rails mailing list for help; I think Rails-core is the right one for this problem.
If you wish to assign an object to a has_one association without saving it, use the build_association method. The object being replaced will still be saved to update its foreign key.
Example
user = User.new
user.build_image
精彩评论