Simple validation problem

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?

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

Vishnu G. 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

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

Vishnu G. 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?

Mike

mike mars wrote:

Vishnu G. 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?

Mike

Or if price.notnil?

Mike

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs