Hi, I need to use validates_uniqueness_of only when inserting new records but not when update them. I use the following line of code, validates_uniqueness_of :type_id, :scope => [:user_name], :unless => :update, :message => "This has already been registered" But still it doesn''t run validations and allow duplicate inserts. Thanks in advance for your feedback. BR, Shiran -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 18, 10:32 am, shiran <shiran.dilru...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I need to use validates_uniqueness_of only when inserting new records > but not when update them. > > I use the following line of code, > > validates_uniqueness_of :type_id, :scope => [:user_name], :unless > => :update, :message => "This has already been registered" > > But still it doesn''t run validations and allow duplicate inserts.Looks like you''re confusing the :unless/:if options (which take a method name or block and run the validation depending on whether the block/method evaluates to true) and the :on option. Fred> > Thanks in advance for your feedback. > > BR, > > Shiran-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
validates_uniqueness_of :type_id, :scope => [:user_name], :message => "This has already been registered", :if => :new_record? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Fred... will look at it On Feb 18, 3:40 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 18, 10:32 am, shiran <shiran.dilru...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I need to use validates_uniqueness_of only when inserting new records > > but not when update them. > > > I use the following line of code, > > > validates_uniqueness_of :type_id, :scope => [:user_name], :unless > > => :update, :message => "This has already been registered" > > > But still it doesn''t run validations and allow duplicate inserts. > > Looks like you''re confusing the :unless/:if options (which take a > method name or block and run the validation depending on whether the > block/method evaluates to true) and the :on option. > > Fred > > > > > Thanks in advance for your feedback. > > > BR, > > > Shiran-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Thanks a lot Sharagoz... On Feb 18, 3:45 pm, Sharagoz -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> validates_uniqueness_of :type_id, :scope => [:user_name], :message => > "This has already been registered", :if => :new_record? > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hi, As you need to fire uniqueness validation during creation of new record. validates_uniqueness_of :type_id, :scope => [:user_name],:on => :create, :message => "This has already been registered" -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Priyanka... On Feb 18, 3:55 pm, Priyanka Pathak <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > As you need to fire uniqueness validation during creation of new > record. > > validates_uniqueness_of :type_id, :scope => [:user_name],:on => > :create, :message => "This has already been registered" > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Another technique which may be overkill for what you need is doing something like this... Say you have a very simple user signup but, after signup they have to fill in a more extensive user profile which has many more fields. I''ve solved this by doing: class User < ActiveRecord::Base validates_presence_of :login, :email validates_presence_of :password, :unless => :password_set validates_presence_of :password_confirm, :unless => :password_set validates_length_of :password, :within => 4..40, :unless => :password_set validates_confirmation_of :password, :unless => :password_set validates_length_of :login, :within => 3..40 validates_length_of :email, :within => 3..100 validates_uniqueness_of :login, :email, :case_sensitive => false validates_as_email :email end # This handles the User Form validations class UserForm < User validates_presence_of :first_name validates_presence_of :last_name end When doing things that require first, last name just use the UserForm object instead. On Thu, Feb 18, 2010 at 3:09 AM, shiran <shiran.dilruwan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Priyanka... > > On Feb 18, 3:55 pm, Priyanka Pathak <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > Hi, > > > > As you need to fire uniqueness validation during creation of new > > record. > > > > validates_uniqueness_of :type_id, :scope => [:user_name],:on => > > :create, :message => "This has already been registered" > > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.