dominik.aumayr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-07 09:21 UTC
Rails Models: Validation!
Hi! I''m reading "Agile Web Development with Rails" at the moment, but now I came to a problem that I can not solve myself. In a Model called "Product" I want to validate the presence of a decimal. I do that with "validates_presence_of". And, additionally, I want to validate that the decimal typed in by the user is greater than zero. I do that with a protected method called "validate". Here is the code for the method "validate": def validate errors.add(:price, "should be at least 0.01") if price.nil? || price < 0.01 end But here comes the problem: If the user does not type in a value, this case should be caught by "price.nil?", because there will be no value be set for it and so it won''t be set at all (i hope that is right! ;) ). And if there is a value, the "if" should check if the price is smaller than 0.01! And if Mister Dave Thomas says so, it should work this way (This code is in his book!)! But the reality looks like the following: (Browser rendered the following, when i tried to let the field empty) "undefined local variable or method ` price'' for #<Product:0x2649c08>" It looks like this does not work (the if-checking thing)! Has anyone a suggestion how i could fix that so the behaviour will be exactly like I expect it...?!? And: Does Rails first execute "validates_presence_of" (and sisters and brothers) or "validate" (which I wrote myself)?? Thanks and Greetings from Austria, - dominik --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dominik.aumayr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi! > > I''m reading "Agile Web Development with Rails" at the moment, but now > I came to a problem that I can not solve myself. > In a Model called "Product" I want to validate the presence of a > decimal. I do that with "validates_presence_of". And, additionally, I > want to validate that the decimal typed in by the user is greater than > zero. I do that with a protected method called "validate". > Here is the code for the method "validate": > def validate > errors.add(:price, "should be at least 0.01") if price.nil? || > price < 0.01 > end > > But here comes the problem: If the user does not type in a value, this > case should be caught by "price.nil?", because there will be no value > be set for it and so it won''t be set at all (i hope that is > right! ;) ). And if there is a value, the "if" should check if the > price is smaller than 0.01! And if Mister Dave Thomas says so, it > should work this way (This code is in his book!)! But the reality > looks like the following: (Browser rendered the following, when i > tried to let the field empty) > "undefined local variable or method ` price'' for #<Product:0x2649c08>" > > It looks like this does not work (the if-checking thing)! > > Has anyone a suggestion how i could fix that so the behaviour will be > exactly like I expect it...?!? > And: Does Rails first execute "validates_presence_of" (and sisters and > brothers) or "validate" (which I wrote myself)?? > > Thanks and Greetings from Austria, > - dominik > > > > > >I think the prob is not with the validate method but with the Product#price attribute. Can you just check that the attribute exists on this model. Go into the console and try Product.new.respond_to?(:price) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---