# Simple validation problem

**URL:** <https://www.ruby-forum.com/t/simple-validation-problem/69441>\
**Category:** Rails\
**Created:** [September 21, 2006, 2:48am UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441 "2006-09-21T02:48:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 21, 2006, 2:48am UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/1 "2006-09-21T02:48:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 21, 2006, 8:02am UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/2 "2006-09-21T08:02:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 26, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/3 "2006-09-26T20:11:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 26, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/4 "2006-09-26T20:39:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 27, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/5 "2006-09-27T17:05:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [September 27, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/simple-validation-problem/69441/6 "2006-09-27T17:08:55Z")

</div>

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
