Validations

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 ?

On Oct 8, 7:48 am, Eric [email protected] wrote:

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

  1. 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