How to does validates_uniqueness with option :unless work?

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

On Apr 16, 6:16 am, user splash [email protected]
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

Frederick C. wrote:

On Apr 16, 6:16�am, user splash [email protected]
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

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs