ruby-on-rails - Rails 错误 : accepts_nested_attributes_for is not updating my has_many association

标签 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

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

"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", "inventory"=>"08", "id"=>"1030"},

当我在 Controller 中执行 @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`.

这是怎么回事?当我创建一个包含许多 BookBookVersion 时,表单工作得非常好。但是,当我使用现有书籍版本更新现有书籍时,它不会更新或验证任何内容。

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

更新 ====

呃...我认为这是 Rails 中的一个错误?看看会发生什么:
    >> @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 之前卡住 Controller 时。当我在更新属性并尝试运行它之前实际将 @book.book_versions 放入 Controller 时,它仍然不起作用。

最佳答案

所以我想出了一些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

关于ruby-on-rails - Rails 错误 : accepts_nested_attributes_for is not updating my has_many association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634107/

相关文章:

ruby-on-rails - 在 Rails 中设置用户和赏金之间的关联

ruby-on-rails - 使用 Mechanize 过滤数组结果

ruby-on-rails - 在后台运行 Webrick 服务器?

ruby-on-rails - 在 Ruby on Rails 中实现复合模式

ruby-on-rails - 按 ActiveRecord 中的多列分组

ruby-on-rails - 查找返回枚举器,而不是对象

sql - Rails 用于创建和更新操作的唯一订单字段

jquery - 使用Rails UJS,如何从函数提交远程表单

ruby-on-rails - docker 下的 .gitignore 文件放在哪里?

ruby-on-rails - 使用 ActiveRecord 冒泡嵌套事务失败