Rails 3: How to disable autosave of has_one and has_many associations?

I have a user model that has an image. how can i disable the autosave option. when I tried the following, I get an error. I use rails 3.

class User < 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):
does autosave not load autosave_association.rb correctly?

+3
source share
2 answers

This will probably not solve your problem, but it may be useful to you anyway:

It 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

no. If you create an instance Post, for example,

post = Post.find 1

You will get the following:

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'
[...] 

, , . has_many, has_one hiccups, ​​ :autosave.

- Qaru - :has_one, Rails ; , Rails-core .

+2

has_one, , build_association. , , .

user = User.new
user.build_image
0

Source: https://habr.com/ru/post/1779433/


All Articles