Yukti Vig
2008-Jan-29 06:23 UTC
Use of validates_uniqueness_of for create but not for edit
Hello People, I wanted to validate my form for duplicate entries while creating a new user . So I added validates_uniqueness_of in the users model. But it gets applied to the edit user code also. Since its an edit-user page, it should not validate duplicate records and should update the existing record.Or say , is should have validations of it own I tried adding a validation function check_duplicate in the model and called it before_create . But that gives errors. Can anyone please help me for this. Thanks n Regards -- 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 -~----------~----~----~----~------~----~------~--~---
validates_uniqueness_of should validate on create and update, to achieve some degree of uniqueness. Assuming your data is unique, am surprised that your model is complaining. Yukti Vig wrote:> Hello People, > > I wanted to validate my form for duplicate entries while creating a new > user . So I added validates_uniqueness_of in the users model. But it > gets applied to the edit user code also. > > Since its an edit-user page, it should not validate duplicate records > and should update the existing record.Or say , is should have > validations of it own > > I tried adding a validation function check_duplicate in the model and > called it before_create . But that gives errors. > > > > Can anyone please help me for this. > > Thanks n Regards-- 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 -~----------~----~----~----~------~----~------~--~---
Yukti Vig
2008-Jan-29 06:54 UTC
Re: Use of validates_uniqueness_of for create but not for e
Thanks Bl Jj for putting your time. before_create :check_duplicate def check_duplicate validates_uniqueness_of :login,:scope =>[:email], :case_sensitive => false end This is what I added to my model. Amd the error I get is undefined method `validates_uniqueness_of'' for #<User:0x3752c24> Is there any issue with the "check_duplicate" not getting values of login and email. regards. Bl Jj wrote:> validates_uniqueness_of should validate on create and update, to achieve > some degree of uniqueness. Assuming your data is unique, am surprised > that your model is complaining.-- 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 -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2008-Jan-29 15:12 UTC
Re: Use of validates_uniqueness_of for create but not for e
On Jan 29, 2008 1:54 AM, Yukti Vig <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > before_create :check_duplicate > > def check_duplicate > validates_uniqueness_of :login,:scope =>[:email], :case_sensitive > => false > end > > This is what I added to my model. Amd the error I get is > > undefined method `validates_uniqueness_of'' for #<User:0x3752c24> > > Is there any issue with the "check_duplicate" not getting values of > login and email.I don''t know if you can use the validates_* methods in callbacks, so I don''t know whether it makes sense for you to have received that error. My first instinct would be to try something like validates_uniqueness_of :login, :on => :create, ... The TextMate snippet for validates_uniqueness_of adds an :on option, but the Rails API docs don''t list one. I don''t have time to actually try it all out here, but maybe you can use the :on option to indicate that the check should be made only on :create. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yukti Vig
2008-Jan-31 06:17 UTC
Re: Use of validates_uniqueness_of for create but not for e
That works !! Craig :)) Thanks allot!!! regards Yukti.Vig Craig Demyanovich wrote:> I don''t know if you can use the validates_* methods in callbacks, so I > don''t > know whether it makes sense for you to have received that error. > > My first instinct would be to try something like > > validates_uniqueness_of :login, :on => :create, ... > > The TextMate snippet for validates_uniqueness_of adds an :on option, but > the > Rails API docs don''t list one. I don''t have time to actually try it all > out > here, but maybe you can use the :on option to indicate that the check > should > be made only on :create.-- 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 -~----------~----~----~----~------~----~------~--~---
Ismail Degani
2008-Oct-19 22:47 UTC
Re: Use of validates_uniqueness_of for create but not for e
Hi, The reason you''re getting this error is because the "validates" functions are class level methods, whereas your check_duplicate function is an instance method. So, to fix this, just reference the "validates_" methods with the appropriate model name: def check_duplicate User.validates_uniqueness_of :login,:scope =>[:email], :case_sensitive end Craig Demyanovich wrote:> On Jan 29, 2008 1:54 AM, Yukti Vig <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> undefined method `validates_uniqueness_of'' for #<User:0x3752c24> >> >> Is there any issue with the "check_duplicate" not getting values of >> login and email. > > > I don''t know if you can use the validates_* methods in callbacks, so I > don''t > know whether it makes sense for you to have received that error. > > My first instinct would be to try something like > > validates_uniqueness_of :login, :on => :create, ... > > The TextMate snippet for validates_uniqueness_of adds an :on option, but > the > Rails API docs don''t list one. I don''t have time to actually try it all > out > here, but maybe you can use the :on option to indicate that the check > should > be made only on :create. > > Regards, > Craig-- 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 -~----------~----~----~----~------~----~------~--~---