Christian Johansen
2006-Dec-16 23:26 UTC
Validate some fields only on object creation, not update
I''m having a problem with my User model. Originally I had alot of validation, including this: validates_presence_of :password_confirmation, :password validates_length_of :password, :within => 4..50 validates_confirmation_of :password However, this made it impossible to edit a user without setting his password, which I don''t want. So I tried to isolate the above validations in the before_create method, but that didn''t work too well: "undefined method validates_presence_of for model <User>" Is there some way I can perform those validations only when a user is created? -- 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 -~----------~----~----~----~------~----~------~--~---
This should work. validates_numericality_of :value, :on => :create On Dec 16, 3:26 pm, Christian Johansen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m having a problem with my User model. Originally I had alot of > validation, including this: > > validates_presence_of :password_confirmation, :password > validates_length_of :password, :within => 4..50 > validates_confirmation_of :password > > However, this made it impossible to edit a user without setting his > password, which I don''t want. So I tried to isolate the above > validations in the before_create method, but that didn''t work too well: > > "undefined method validates_presence_of for model <User>" > > Is there some way I can perform those validations only when a user is > created? > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
validates_presence_of :password_confirmation, :password, :on => :create --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---