Hi guys, I have given a validation function (validates_uniqueness_of :email) in my model because i want email to be unique. So the problem is that whenever i want to update a record then also it validates. I mean i dont want it to validate while updating the record. It should happen only when i am creating a new record. I tried these methods, 1) :on => { :create } options is not working. 2) before_save :check_duplication def check_duplication ModelName.validates_**_of :email end But still snatching my head over it.. IF these two ways are not working what way it will work? -- 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.
I dont understand why you want to validate uniqueness only on create. Isnt it a problem if email is changed later to an address that already exists? The :on parameter is not available for validates_uniqueness_of, probably because it doesnt make any sense. If you really want to make the validation happen only on create you can do before_validation_on_create :check_duplication -- 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.
On Thu, Feb 11, 2010 at 05:37, Hemant Bhargava <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi guys, > > I have given a validation function (validates_uniqueness_of :email) in > my model because i want email to be unique. > > So the problem is that whenever i want to update a record then also it > validates. I mean i dont want it to validate while updating the record. > It should happen only when i am creating a new record. > > I tried these methods, > > 1) :on => { :create } options is not working. > 2) before_save :check_duplication > > def check_duplication > ModelName.validates_**_of :email > end > > But still snatching my head over it.. IF these two ways are not working > what way it will work? > -- > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >The reason that your approach doesn''t work is that you''re still calling validates_uniqueness_of on your class. This is telling it, "from now on, check uniqueness of this column any time the record is saved". In effect, it''s no different from just putting that code directly in the class body. You can use the :if option: class ModelName validates_uniqueness_of :email, :if => proc { |model| model.new_record? } end -- 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.
> > The reason that your approach doesn''t work is that you''re still > calling validates_uniqueness_of on your class. This is telling it, > "from now on, check uniqueness of this column any time the record is > saved". In effect, it''s no different from just putting that code > directly in the class body. > > You can use the :if option: > > class ModelName > validates_uniqueness_of :email, :if => proc { |model| > model.new_record? } > endThanks Resolved .. worked like a charm .. :) Cheers .. -- 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.
On Feb 11, 12:51 pm, Mat Brown <m...-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote:> > class ModelName > validates_uniqueness_of :email, :if => proc { |model| model.new_record? } > endand in cases like this where you just want to call a method you can do :if => :new_record? 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-/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.
On Thu, Feb 11, 2010 at 11:57, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Feb 11, 12:51 pm, Mat Brown <m...-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote: >> >> class ModelName >> validates_uniqueness_of :email, :if => proc { |model| model.new_record? } >> end > > and in cases like this where you just want to call a method you can > do :if => :new_record? > > 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-/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.Haha, duh, my bad. -- 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.