Hi all! I''m trying to make a model validation that starts only if a given attribute has a specified value, but it seems i''m doing something wrong :) I have 3 values: respond_via, email, telephone_number. I want the latter ones to validate only if respond_via has specific value. So far i have: validates_presence_of :email, :if => (:respond_via == "e-mail") validates_presence_of :telephone_number, :if => (:respond_via ="telephone") , but this version raises undefined method `respond_via'' exception. I am sure it is defined, so it must be my syntax that is to blame. Any suggestions? -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/23/07, Mind Mage <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi all! > I''m trying to make a model validation that starts only if a given > attribute has a specified value, but it seems i''m doing something wrong > :) > > I have 3 values: respond_via, email, telephone_number. I want the latter > ones to validate only if respond_via has specific value. > > So far i have: > validates_presence_of :email, :if => (:respond_via == "e-mail")validates_presence_of :email :if => Proc.new {|model| model.respond_via == "e_mail"} -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Rick Denatale wrote:> validates_presence_of :email :if => Proc.new {|model| > model.respond_via == "e_mail"}Thanks a lot, this worked :) -- 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 -~----------~----~----~----~------~----~------~--~---