Ticket 11394 adds the merge! method to any ActiveRecord::Errors
object. If anyone has time to review the ticket and post their
comments on if they like the idea of having this functionality:
I think the example is too contrived. That particular situation you
would/could solve using errors_for 'person', 'address'. A better
example would be something that uses delegation.
class ZipCode < ARec::Base
has_many :addresses
validates_presence_of :city, :state, :zip
end
class Address < ARec::Base
belongs_to :zip_code
validates_presence_of :street
validates_associated :zip_code
[:city, :state, :zip].each do |delegated_attribute|
delegate delegated_attribute,
"#{delegated_attribute}=", :to=>:zip_code
end
def initialize(*attrs)
params = attrs.last.is_a?(Hash) ? attrs.last : {}
self.zip_code = params[:zip_code_id].nil? ? ZipCode.new :
ZipCode.find(params[:zip_code_id])
super
end
end
home = Address.new :street=>'123 Main
St', :city=>'Anytown', :state=>'SC'
home.valid?
#=> false
In this scenario it would be helpful to merge the errors from home and
home.zip_code since they appear to be one object to the user.