# Errors.each\_key throws undefined method error

**URL:** <https://www.ruby-forum.com/t/errors-each-key-throws-undefined-method-error/173667>\
**Category:** Rails\
**Created:** [August 13, 2009, 2:04am UTC](https://www.ruby-forum.com/t/errors-each-key-throws-undefined-method-error/173667 "2009-08-13T02:04:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [August 13, 2009, 2:04am UTC](https://www.ruby-forum.com/t/errors-each-key-throws-undefined-method-error/173667/1 "2009-08-13T02:04:50Z")

</div>

I’m trying to create custom error messages and in doing so I thought I  
could access the validation errors each\_key method. I know each\_key  
isn’t listed as a method for errors object but errors object is a hash  
right? When I look at the source of each method

def each  
@errors.each\_key { |attr| @errors[attr].each { |msg| yield attr, msg }  
}  
end

each\_key is being used but when I access it directly, it barfed with  
undefined method error. Anyone knows why? Thanks in advance for your  
help.

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [August 13, 2009, 1:40pm UTC](https://www.ruby-forum.com/t/errors-each-key-throws-undefined-method-error/173667/2 "2009-08-13T13:40:12Z")

</div>

@errors is an instance variable on the model - I doubt you’re  
accessing that from the controller…

I’m also not clear what’s wrong with @model.errors.each - it gets you  
exactly the same results as iterating with each\_key on the underlying  
array…

–Matt J.

On Aug 12, 8:04 pm, Cali W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [August 13, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/errors-each-key-throws-undefined-method-error/173667/3 "2009-08-13T17:52:16Z")

</div>

Matt J. wrote:

> @errors is an instance variable on the model - I doubt you’re  
> accessing that from the controller…
> 
> I’m also not clear what’s wrong with @model.errors.each - it gets you  
> exactly the same results as iterating with each\_key on the underlying  
> array…
> 
> –Matt J.
> 
> On Aug 12, 8:04ï¿½pm, Cali W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

There isn’t anything wrong with @model.errors.each, I was experimenting  
with different ways to get at the unique keys since there may be more  
than 1 error message per key (field). I ended up creating a local hash  
variable to get the functionality I want. Thanks for your insight.
