# Validates\_uniqueness\_of problem

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298>\
**Category:** Rails\
**Created:** [February 11, 2010, 11:37am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298 "2010-02-11T11:37:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 11:37am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/1 "2010-02-11T11:37:31Z")

</div>

Hi guys,

I have given a validation function (validates\_uniqueness\_of :email) in  
my model because i want email to be unique.

So the problem is that whenever i want to update a record then also it  
validates. I mean i dont want it to validate while updating the record.  
It should happen only when i am creating a new record.

I tried these methods,

1. :on =\> { :create } options is not working.

2. before\_save :check\_duplication

But still snatching my head over it… IF these two ways are not working  
what way it will work?

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 12:11pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/2 "2010-02-11T12:11:03Z")

</div>

I dont understand why you want to validate uniqueness only on create.  
Isnt it a problem if email is changed later to an address that already  
exists?

The :on parameter is not available for validates\_uniqueness\_of, probably  
because it doesnt make any sense.

If you really want to make the validation happen only on create you can  
do before\_validation\_on\_create :check\_duplication

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 1:53pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/3 "2010-02-11T13:53:01Z")

</div>

On Thu, Feb 11, 2010 at 05:37, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

The reason that your approach doesn’t work is that you’re still  
calling validates\_uniqueness\_of on your class. This is telling it,  
“from now on, check uniqueness of this column any time the record is  
saved”. In effect, it’s no different from just putting that code  
directly in the class body.

You can use the :if option:

class ModelName  
validates\_uniqueness\_of :email, :if =\> proc { |model|  
model.new\_record? }  
end

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/4 "2010-02-11T17:58:54Z")

</div>

On Feb 11, 12:51 pm, Mat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class ModelName  
> validates\_uniqueness\_of :email, :if =\> proc { |model| model.new\_record? }  
> end

and in cases like this where you just want to call a method you can  
do :if =\> :new\_record?

Fred

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/5 "2010-02-11T15:27:55Z")

</div>

> The reason that your approach doesn’t work is that you’re still  
> calling validates\_uniqueness\_of on your class. This is telling it,  
> “from now on, check uniqueness of this column any time the record is  
> saved”. In effect, it’s no different from just putting that code  
> directly in the class body.
> 
> You can use the :if option:
> 
> class ModelName  
> validates\_uniqueness\_of :email, :if =\> proc { |model|  
> model.new\_record? }  
> end

Thanks Resolved … worked like a charm … 🙂

Cheers …

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 11, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-problem/183298/6 "2010-02-11T18:03:35Z")

</div>

On Thu, Feb 11, 2010 at 11:57, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fred
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

Haha, duh, my bad.
