# Validates\_each magic: How?

**URL:** <https://www.ruby-forum.com/t/validates-each-magic-how/179580>\
**Category:** Rails\
**Created:** [November 29, 2009, 11:49am UTC](https://www.ruby-forum.com/t/validates-each-magic-how/179580 "2009-11-29T11:49:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 11:49am UTC](https://www.ruby-forum.com/t/validates-each-magic-how/179580/1 "2009-11-29T11:49:03Z")

</div>

I have an attribute :x in a form

validates\_each manages to be able to get to the value of :x in the form.

(Source for validates\_each from [http://api.rubyonrails.org/](http://api.rubyonrails.org/))

```
 # File vendor/rails/activerecord/lib/active_record/validations.rb,

```

line 390  
390: def validates\_each(\*attrs)  
391: options = attrs.extract\_options!.symbolize\_keys  
392: attrs = attrs.flatten  
393:  
394: # Declare the validation.  
395: send(validation\_method(options[:on] || :save), options) do  
|record|  
396: attrs.each do |attr|  
397: value = record.send(attr)  
398: next if (value.nil? && options[:allow\_nil]) ||  
(value.blank? && options[:allow\_blank])  
399: yield record, attr, value  
400: end  
401: end  
402: end

I think I want to do  
record.send(:x)  
but I am unable to follow the logic to understand where record comes  
from.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/validates-each-magic-how/179580/2 "2009-11-29T16:50:40Z")

</div>

Ralph S. wrote:

> I have an attribute :x in a form
> 
> validates\_each manages to be able to get to the value of :x in the form.

[…]

> I think I want to do  
> record.send(:x)

Why do you think you want to do that? What are you trying to achieve?  
How are we supposed to help with so little information? 🙂

> but I am unable to follow the logic to understand where record comes  
> from.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/validates-each-magic-how/179580/3 "2009-11-29T18:17:42Z")

</div>

Marnen Laibow-Koser wrote:

> Ralph S. wrote:
> 
> > I have an attribute :x in a form
> > 
> > validates\_each manages to be able to get to the value of :x in the form.
> 
> […]
> 
> > I think I want to do  
> > record.send(:x)
> 
> Why do you think you want to do that? What are you trying to achieve?  
> How are we supposed to help with so little information? 🙂
> 
> > but I am unable to follow the logic to understand where record comes  
> > from.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

I am trying to understand the magic of validates\_each. I have traced  
the logic to the point where it does a  
record.send(:x)  
and I am trying to understand where the entity “record” comes from so  
that I can capture the value in the form associate with the symbol :x.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/validates-each-magic-how/179580/4 "2009-11-29T18:37:49Z")

</div>

Ralph S. wrote:  
[…]

> I am trying to understand the magic of validates\_each. I have traced  
> the logic to the point where it does a  
> record.send(:x)  
> and I am trying to understand where the entity “record” comes from so  
> that I can capture the value in the form associate with the symbol :x.

As an academic exercise, this may be valuable. For practical purposes,  
you probably don’t need to do this – just look in the params hash.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
