开发者

incompatibility for accepts_nested_attributes_for between rails 2.3.5 and rails 2.3.8

I just discovered a change of behaviour between rails 2.3.5 and rails 2.3.8 and I can not find anything googling.

I have to models

class Book < ActiveRecord::Base
  has_many :authors
  accepts_nested_attributes_for authors
end

class Author < ActiveRecord::Base
  belongs_to :book
end

I have a view to fill a book with authors nested views.

Into book controller I have an update action

def update
  @book = Book.find(params[:id])
  @book.attributes = params[:book]
  ....
end

params[:book] look likes this :

{:name=>"a great book", "authors_attributes"=>{"1"=>{:id"=>"2", "_destroy"=>"", :name => "Boney M"}}}

Latter in my proccess, I want to access to my updated collection (before saving the book object, for validation for example), with rails 2.3.5 @book.authors gave me the authors collection updated with the value into params[:book] hash. Bu开发者_Go百科t with 2.3.8, it gives me the author collection reloaded from database.

My problem comes from function assign_nested_attributes_for_collection_association (/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb). It returns a AssociationProxy object with @loaded = false. Thus the first time I want to access my collection with @book.authors, it reloads it from db. This was not the case with rails 2.3.5.

Did I missed something here or this new behaviour has been reported somewhere ?

Thanks in advance for your help.


Not sure if this is related, but I just noticed that when I updated my app from 2.3.5 to 2.3.8 I can no longer update the associated record. The system ignores updated information for already existing records and will only add new records. I find that if I want to update the associated record I have to remove it from the db and then create it again. Totally sucks.

class Product < ActiveRecord::Base
    has_many :allowed_design_types, :dependent => :destroy, :include => [:design_type], :order => 'design_types.name'
    has_many :design_types, :through => :allowed_design_types
    accepts_nested_attributes_for(
      :allowed_design_types, 
      :allow_destroy => true, 
      :reject_if => proc { |obj| p obj; !['1', 'true'].include?(obj.delete('allow')) }
    )
end

This gets sent in the params but gets completely ignored:

"product"=>{"allowed_design_types_attributes"=>{"0"=>{"design_type_id"=>"5", "allow"=>"1", "product_id"=>"1", "id"=>"45", "your_price"=>"3", "_destroy"=>""}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜