# Ticket #11394, Adding ActiveRecord::Errors#merge! method

**URL:** <https://www.ruby-forum.com/t/ticket-11394-adding-activerecord-errors-merge-method/133080>\
**Category:** Rails\
**Created:** [March 27, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/ticket-11394-adding-activerecord-errors-merge-method/133080 "2008-03-27T15:12:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdennis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zdennis](https://www.ruby-forum.com/u/zdennis)\
**Post date:** [March 27, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/ticket-11394-adding-activerecord-errors-merge-method/133080/1 "2008-03-27T15:12:09Z")

</div>

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:

[http://dev.rubyonrails.org/ticket/11394](http://dev.rubyonrails.org/ticket/11394)

Examples:  
person.errors.merge address.errors  
person.errors.merge address.errors, :only =\> [:city, ;state]  
person.errors.merge address.errors, :except =\> [:street, :zip]

Thanks,

Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

---

<div class="post-metadata">

**Author:** ![zdennis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zdennis](https://www.ruby-forum.com/u/zdennis)\
**Post date:** [March 27, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/ticket-11394-adding-activerecord-errors-merge-method/133080/2 "2008-03-27T15:14:01Z")

</div>

Those examples should be “merge!” and not “merge”

Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

---

<div class="post-metadata">

**Author:** ![zdennis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zdennis](https://www.ruby-forum.com/u/zdennis)\
**Post date:** [March 27, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/ticket-11394-adding-activerecord-errors-merge-method/133080/3 "2008-03-27T16:15:02Z")

</div>

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.
