我有2種型號:Rails的錯誤:accepts_nested_attributes_for沒有更新我的has_many協會
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
這裏是我的PARAMS。請注意,我如何將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`.
發生了什麼事?當我創建一個Book
與許多BookVersion
s形式工作完全正常。但是,當我使用現有書籍版本更新現有書籍時,它不會更新或驗證任何內容。
這是我的問題在這裏的延續:ActiveRecord: validates_associated does not work when updating model?
UPDATE ====
呃......我認爲這是在軌道中的錯誤?看看會發生什麼:
>> @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
置於控制器中,但它仍然不起作用。