# How to does validates\_uniqueness with option :unless work?

**URL:** <https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922>\
**Category:** Rails\
**Created:** [April 16, 2008, 7:16am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922 "2008-04-16T07:16:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 16, 2008, 7:16am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922/1 "2008-04-16T07:16:44Z")

</div>

Hi,

I am quite confuse with validates\_uniqueness with option :unless.  
Correct me if I’m wrong, the :unless =\> :method option enables  
validation to happen to all except that particular method. But when I  
used the unless option, it seems to have stop the validating of  
uniqueness for all other methods, like :save or :update. Is there a way  
to not validate uniqueness in only one action?

Thanks.

Example:  
…In a model…  
validates\_uniqueness\_of :name, :unless =\> :deep\_clone

def deep\_clone

```
 result = self.clone

 self.children.each { |c| result.children << c.deep_clone}

 result

```

end

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 16, 2008, 10:05am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922/2 "2008-04-16T10:05:31Z")

</div>

On Apr 16, 6:16 am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I am quite confuse with validates\_uniqueness with option :unless.  
> Correct me if I’m wrong, the :unless =\> :method option enables  
> validation to happen to all except that particular method. But when I  
> used the unless option, it seems to have stop the validating of  
> uniqueness for all other methods, like :save or :update. Is there a way  
> to not validate uniqueness in only one action?

unless means the validation does not run if the specified method  
returns true. Your deep\_clone method always returns true so the  
validation never runs. It has nothing to do with who caused the  
validation to be called or which controller action is being processed.

Fred

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 17, 2008, 10:34am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922/3 "2008-04-17T10:34:54Z")

</div>

Frederick C. wrote:

> On Apr 16, 6:16ï¿½am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:  
> unless means the validation does not run if the specified method  
> returns true. Your deep\_clone method always returns true so the  
> validation never runs. It has nothing to do with who caused the  
> validation to be called or which controller action is being processed.
> 
> Fred

Hi,

So is it right to say that if the specified method returns true, the  
validation does not run at all?

What should I do if I want the validation to run only when I create or  
update a record and not when I copy a record? Can I still use the unless  
option? If yes how?

Thanks

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 18, 2008, 5:09am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922/4 "2008-04-18T05:09:20Z")

</div>

Frederick C. wrote:

> On 17 Apr 2008, at 09:34, user splash wrote:
> 
> > 
> 
> There’s the :on option which distinguishes between updates and  
> creates, but that won’t help you here (since copy isn’t something AR  
> knows about).
> 
> Have a method that returns true when that is the case (that method can  
> be as simple as an attr\_accessor which you set to true when relevant)
> 
> Fred

Thanks Fred. It works.

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 17, 2008, 11:54am UTC](https://www.ruby-forum.com/t/how-to-does-validates-uniqueness-with-option-unless-work/134922/5 "2008-04-17T11:54:17Z")

</div>

On 17 Apr 2008, at 09:34, user splash wrote:

> > Fred
> 
> Hi,
> 
> So is it right to say that if the specified method returns true, the  
> validation does not run at all?  
> yes

> What should I do if I want the validation to run only when I create or  
> update a record and not when I copy a record? Can I still use the  
> unless  
> option? If yes how?

There’s the :on option which distinguishes between updates and  
creates, but that won’t help you here (since copy isn’t something AR  
knows about).

Have a method that returns true when that is the case (that method can  
be as simple as an attr\_accessor which you set to true when relevant)

Fred
