OK so...Im reading the book agile web development with rails, and i have 2 questions... 1.Why does this :with => %r{\.(gif|jpg|png)$}i work ? I know %r{\.(gif|jpg|png)$} this checks the extension but why %r{\. $} ? 2. If i check this validates_numericality_of :price why do i have to check in here: def validate errors.add(:price, "should be at least 0.01" ) if price.nil? || price < 0.01 end if price.nil? ? Why do i have to check that ? Isnt that checked with validates_numericality_of :price ? -- 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 -~----------~----~----~----~------~----~------~--~---
On Oct 8, 7:48 am, Eric <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> OK so...Im reading the book agile web development with rails, and i have > 2 questions... > > 1.Why does this :with => %r{\.(gif|jpg|png)$}i work ? I know > %r{\.(gif|jpg|png)$} this checks the extension but why %r{\. $} ? > 2. If i check this validates_numericality_of :price why do i have to > check in here: > def validate > errors.add(:price, "should be at least 0.01" ) if price.nil? || price > < 0.01 > end > > if price.nil? ? Why do i have to check that ? Isnt that checked with > validates_numericality_of :price ? > > -- > Posted viahttp://www.ruby-forum.com/.1. %r{} is another way of specifying a regex 2. having the validation check for price.nil? will result in this message being added when the price is .... nil. You should get two validation errors when price is nil, one complaining that it isn''t a number and one saying it should be at least 0.01. _Kevin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> 2. If i check this validates_numericality_of :price why do i have to > check in here: > def validate > errors.add(:price, "should be at least 0.01" ) if price.nil? || > price > < 0.01 > end > > if price.nil? ? Why do i have to check that ? Isnt that checked with > validates_numericality_of :price ?Hi, You can use validates_numericality_of :price, :allow_nil=>false --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I wrote a model, who save two attributes in the database. the first attribute is a date and the second a describtion. I wrote a validation, who check every date. I would only save a record, when the date is greater than the saved one. That''s running now - yupie! But I would like to update the describtion of every record after the event. Thats the problem. My validation didn''t accept the update, because the date is younger than the last saved one. So I would the validation, but I also want to update every record. Thanks for your help! -- 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 -~----------~----~----~----~------~----~------~--~---
M. R. wrote:> But I would like to update the describtion of every record after the > event. Thats the problem. My validation didn''t accept the update, > because the date is younger than the last saved one.The solution is validate_on_create. The validation block runs only in the creation phase. So the problem is solved. -- 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 -~----------~----~----~----~------~----~------~--~---