I can''t seem to get this rule working... I want to validate the numericality of a number only if the number isn''t nil. if not nil, ensure the number is > -180 and less than 180 tips pls? -- 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 -~----------~----~----~----~------~----~------~--~---
validates_numericality_of :price protected def validate errors.add(:price, "error mesage") unless price.nil? || price >= -180 || price <= 180 end Adapted from ze Rails book. Should work, Vish On 9/21/06, Robbie Shepherd <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I can''t seem to get this rule working... I want to validate the > numericality of a number only if the number isn''t nil. if not nil, > ensure the number is > -180 and less than 180 > > tips pls? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Vishnu Gopal wrote:> validates_numericality_of :price > > protected > def validate > errors.add(:price, "error mesage") unless price.nil? || price >= -180 || > price <= 180 > end > > Adapted from ze Rails book. > Should work, > VishI think you should put if instead of unless. Mike Mars -- 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 -~----------~----~----~----~------~----~------~--~---
errors.add(:price, "error mesage") unless price.nil? || price >= -180 && price <= 180 The test won''t happen if price.nil? which is logical. validates_existence_of could take into account that condition as well. Vish On 9/26/06, mike mars <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Vishnu Gopal wrote: > > validates_numericality_of :price > > > > protected > > def validate > > errors.add(:price, "error mesage") unless price.nil? || price >= -180 || > > price <= 180 > > end > > > > Adapted from ze Rails book. > > Should work, > > Vish > > I think you should put if instead of unless. > > Mike Mars > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Vishnu Gopal wrote:> errors.add(:price, "error mesage") unless price.nil? || price >= -180 && > price > <= 180 > > The test won''t happen if price.nil? which is logical. > validates_existence_of > could take into account that condition as well. > > VishAlso try if price.nil?.false? Mike -- 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 -~----------~----~----~----~------~----~------~--~---
mike mars wrote:> Vishnu Gopal wrote: >> errors.add(:price, "error mesage") unless price.nil? || price >= -180 && >> price >> <= 180 >> >> The test won''t happen if price.nil? which is logical. >> validates_existence_of >> could take into account that condition as well. >> >> Vish > > Also try if price.nil?.false? > > MikeOr if price.notnil? Mike -- 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 -~----------~----~----~----~------~----~------~--~---