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

时间:2014-02-07 17:28:49

标签: ruby-on-rails activerecord

我有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

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

“book”=&gt; {“title”=&gt;“zzzzzz”,“subtitle”=&gt;“”,“author_ids”=&gt; [“”],“illustrator_ids”=&gt; [“”] ,“award_ids”=&gt; [“”],“theme_ids”=&gt; [“”],“publication_date”=&gt;“”,“imprint_id”=&gt;“1”,“language_ids”=&gt; [“ “],”eng_vers_id“=&gt;”“,”book_versions_attributes“=&gt; {”0“=&gt; {”book_id“=&gt;”2848“,”name“=&gt;”alt“,”isbn“= &gt;“”,“price”=&gt;“”,“famis_number”=&gt;“”,“famis_price”=&gt;“”,“weight_in_pounds”=&gt;“”},“1”=&gt; {“ book_id“=&gt;”2848“,”name“=&gt;”bb“,”isbn“=&gt;”123123123123“,”price“=&gt;”“,”famis_number“=&gt;”“,”famis_price“ =&gt;“”,“weight_in_pounds”=&gt;“1.0”,“库存”=&gt;“08”,“id”=&gt;“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: #<BigDecimal:7fc484885b80,'0.1122E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc4848861c0,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #<BigDecimal:7fc484886670,'0.2244E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc484886760,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #<BigDecimal:7fc4848869e0,'0.1212E4',9(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc484886d28,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>]
    >> @book.book_versions.map(&:price)
    => [#<BigDecimal:7fc484885b80,'0.1122E2',18(18)>, #<BigDecimal:7fc484886670,'0.2244E2',18(18)>, #<BigDecimal:7fc4848869e0,'0.1212E4',9(18)>]
    >> @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`.

发生了什么事?当我创建一个包含许多Book的{​​{1}}时,表单完全正常。但是,当我使用现有图书版本更新现有图书时,它不会更新或验证任何内容。

这是我的问题的延续:ActiveRecord: validates_associated does not work when updating model?

更新====

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

BookVersion

但这只是在我使用更好的错误并在 >> @book.update_attributes(params[:book]) => true >> @book.book_versions => [#<BookVersion id: 1030, book_id: 2848, name: "bb", isbn: "123123123123", inventory: "08", price: #<BigDecimal:7fc487ee9488,'0.1122E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee9118,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #<BigDecimal:7fc487ee8f88,'0.2244E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee8e98,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #<BigDecimal:7fc487ee8b50,'0.1212E4',9(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee89c0,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>] >> @book.update_attributes(params[:book]) => false 之前冻结控制器时。当我在更新属性并尝试运行它之前实际将update_attributes放入控制器时,它仍然不起作用。

1 个答案:

答案 0 :(得分:2)

所以我想出了一些hackery ...出于某种原因你必须首先将它们加载到内存中。为了做到这一点,你必须在书籍版本的数组上执行某种功能。仅仅呼叫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