【发布时间】:2014-02-08 06:29:03
【问题描述】:
我有 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 :price 验证(但它不会):
"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”},
当我在控制器中执行@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`.
发生了什么事?当我创建一个带有许多 BookVersions 的 Book 时,该表单工作得非常好。但是,当我使用现有图书版本更新现有图书时,它不会更新或验证任何内容。
这是我的问题的延续: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 之前冻结控制器时。当我实际上在更新属性并尝试运行它之前将@book.book_versions 放入控制器中时,它仍然不起作用。
【问题讨论】:
标签: ruby-on-rails activerecord