2011-03-23 101 views
0

我剛剛發現rails 2.3.5和rails 2.3.8之間的行爲發生了變化,我找不到任何使用Google的東西。對rails 2.3.5和rails之間的accepters_nested_attributes_for的兼容性2.3.8

我不得不車型

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

class Author < ActiveRecord::Base 
    belongs_to :book 
end 

我有一個觀點來寫一本書與嵌套視圖作者。

成冊控制器我有一個更新動作

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

params[:book]看看喜歡這樣的:

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

後者我proccess,我要訪問我的更新集合(保存書對象之前,例如驗證),使用rails 2.3.5 @ book.authors爲我提供了作者集合,其值更新爲params [:book] hash。但是在2.3.8中,它給了我從數據庫重新加載的作者集合。

我的問題來自函數assign_nested_attributes_for_collection_association(/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb)。它用@loaded = false返回一個AssociationProxy對象。因此,我第一次使用@ book.authors訪問我的集合時,它會從db重新加載它。對於rails 2.3.5,情況並非如此。

我在這裏遺漏了什麼,或者這個新行爲已經報道過某處?

在此先感謝您的幫助。

回答

0

不知道這是否有關,但我只是注意到,當我從2.3.5更新我的應用程序2.3.8我不能再更新相關的記錄。系統會忽略已有記錄的更新信息,並且只會添加新記錄。我發現,如果我想更新關聯的記錄,我必須從數據庫中刪除它,然後再次創建它。完全吮吸。

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 

這在PARAMS被髮送,但被完全忽視:

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