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 weird. If I create a Hash with attributes that don't meet the validation criteria of my nested model (such as having a nil card_number), and then try to do an update_attributes call, then what I get returned in a Wallet object with an invalid CreditCard nested model, and the appropriate errors. That is the correct, expected behavior.
If I take that same Hash though and run assign_attributes, and then save (which is all that update_attributes should be doing, then I get returned an invalid Wallet object with a completely nil nested object. Why is that? And how can I update all of the nested attribute values and check for errors without saving?