user splash
2008-Apr-16 05:16 UTC
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-16 08:04 UTC
Re: How to does validates_uniqueness with option :unless work ?
On Apr 16, 6:16 am, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-17 08:34 UTC
Re: How to does validates_uniqueness with option :unless wor
Frederick Cheung wrote:> On Apr 16, 6:16�am, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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. > > FredHi, 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-17 09:53 UTC
Re: How to does validates_uniqueness with option :unless wor
On 17 Apr 2008, at 09:34, user splash wrote:> > Frederick Cheung wrote: >> On Apr 16, 6:16�am, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-18 03:09 UTC
Re: How to does validates_uniqueness with option :unless wor
Frederick Cheung 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) > > FredThanks Fred. It works. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---