Rails bug:accepts_nested_attributes_for没有更新我的has_many关联

我有2个型号:

class Book < ActiveRecord::Base has_many :book_versions accepts_nested_attributes_for :book_versions, allow_destroy: true validates_associated :book_versions class BookVersion < ActiveRecord::Base has_many :collection_items has_many :collections, through: :collection_items belongs_to :book validates_presence_of :price, :isbn #<-- validates presence 

这是我的参数。 请注意我如何将book_version的price保留为名称bb空。 这应该是在BookVersion模型中触发validates_presence_of :pricevalidation(但它没有):

“book”=> {“title”=>“zzzzzz”,“subtitle”=>“”,“author_ids”=> [“”],“illustrator_ids”=> [“”],“award_ids”=> [“ “],”theme_ids“=> [”“],”publication_date“=>”“,”imprint_id“=>”1“,”language_ids“=> [”“],”eng_vers_id“=>”“,”book_versions_attributes “=> {”0“=> {”book_id“=>”2848“,”name“=>”alt“,”isbn“=>”“,”price“=>”“,”famis_number“=>” “,”famis_price“=>”“,”weight_in_pounds“=>”“},”1“=> {”book_id“=>”2848“,”name“=>”bb“,”isbn“=>”123123123123 “,”“price”=>“”,“famis_number”=>“”,“famis_price”=>“”,“weight_in_pounds”=>“1.0”,“库存”=>“08”,“id”=>“ 1030″ },

当我在我的控制器中执行@book.update_attributes(params[:book])时,即使一切看起来都有效,也没有book_versions更新:

  >> @book.update_attributes(params[:book]) => true >> @book.book_versions => [#<BookVersion id: 1030, book_id: 2848, name: "bb", isbn: "123123123123", inventory: "08", price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>] >> @book.book_versions.map(&:price) => [#, #, #] >> @book.book_versions.map(&:price).map(&:to_f) => [11.22, 22.44, 1212.0] >> @book.save => true >> @book.book_versions.map(&:price).map(&:to_f) => [11.22, 22.44, 1212.0] #<-- one of these should be `nil`. 

这是怎么回事? 当我创建一个包含许多BookVersionBookBookVersion 。 但是,当我使用现有书籍版本更新现有书籍时,它不会更新或validation任何内容。

这是我的问题的延续: ActiveRecord:validates_associated在更新模型时不起作用?

更新====

呃……我认为这是铁轨中的一个错误? 看看会发生什么:

  >> @book.update_attributes(params[:book]) => true >> @book.book_versions => [#<BookVersion id: 1030, book_id: 2848, name: "bb", isbn: "123123123123", inventory: "08", price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #, famis_price: nil, famis_number: "", weight_in_pounds: #, width: nil, height: nil, thickness: nil>] >> @book.update_attributes(params[:book]) => false 

但这只是在我使用更好的错误并在update_attributes之前冻结控制器时。 当我在更新属性并尝试运行它之前实际将@book.book_versions放在控制器中时,它仍然不起作用。

所以我想出了一些hackery …出于某种原因你必须首先将它们加载到内存中。 为了做到这一点,你必须在书籍版本的数组上执行某种function。 只是调用book.book_versions是不够的:

  @book.book_versions.sort_by(&:name) # this line is to load the book_versions, without it book_versions will not update!! it's a bug in rails I discovered if @book.update_attributes(params[:book]) #<-- returns false finally