# Validator to ensure two fields are different?

**URL:** <https://www.ruby-forum.com/t/validator-to-ensure-two-fields-are-different/52122>\
**Category:** Rails\
**Created:** [January 11, 2006, 4:09am UTC](https://www.ruby-forum.com/t/validator-to-ensure-two-fields-are-different/52122 "2006-01-11T04:09:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [January 11, 2006, 4:09am UTC](https://www.ruby-forum.com/t/validator-to-ensure-two-fields-are-different/52122/1 "2006-01-11T04:09:31Z")

</div>

What validator can I use in a model to ensure that my :username field is  
different from my :password field?

I’ve tried

validates\_exclusion\_of :password, :in =\>:username

but I get a rails error  
“an object with the method include? is required must be supplied as the  
:in  
option of the configuration hash”

Thanks!

Gary

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [January 11, 2006, 4:38am UTC](https://www.ruby-forum.com/t/validator-to-ensure-two-fields-are-different/52122/2 "2006-01-11T04:38:29Z")

</div>

Gary H. wrote:

> What validator can I use in a model to ensure that my :username field is  
> different from my :password field?
> 
> I’ve tried
> 
> validates\_exclusion\_of :password, :in =\>:username
> 
> but I get a rails error  
> “an object with the method include? is required must be supplied as the  
> :in  
> option of the configuration hash”
> 
> Thanks!
> 
> Gary

try…

validates\_exclusion\_of :password, :in=\>[username]

this method requires an enumerable object for :in. A symbol (:username)  
is not enumerable, an array is.

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [January 12, 2006, 12:23am UTC](https://www.ruby-forum.com/t/validator-to-ensure-two-fields-are-different/52122/3 "2006-01-12T00:23:22Z")

</div>

----- Original Message -----  
From: “Kevin O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, January 10, 2006 10:38 PM  
Subject: [Rails] Re: validator to ensure two fields are different?

> > :in  
> > this method requires an enumerable object for :in. A symbol (:username)  
> > is not enumerable, an array is.

I tried

```
validates_exclusion_of :password, :in=>[username]

```

and that errors with “undefined local variable”

I also tried

```
validates_exclusion_of :password, :in=>[:username]

```

and that does not error but does not catch the fields being equal.

If you have any other suggestions that would be great!

Gary H.
