I have 2 models:
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
Here are my options. Notice how I leave pricebook_version with the name bbempty. This should trigger a check validates_presence_of :pricein the model BookVersion(but it is not):
"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 ?
====
... , ? , :
>> @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 , .