I have two models with the following structure:
class Wallet < ActiveRecord::Base
include ActiveModel::Validations
has_one :credit_card
accepts_nested_attributes_for :credit_card
validates :credit_card, :presence => true
validates_associated :credit_card
...
end
class CreditCard < ActiveRecord::Base
include ActiveModel::Validations
belongs_to :wallet
validates :card_number, :presence => true
validates :expiration_date, :presence => true
...
end
I am testing the functionality of my application with RSpec and I noticed something strange. If I create a hash with attributes that do not meet the criteria for checking my nested model (for example, nil card_number), and then try to make a call update_attributes, then what I get in the Wallet object with an invalid nested CreditCard model and the corresponding errors. This is the correct, expected behavior.
Hash assign_attributes, save ( , update_attributes, . ? ?