# Conditional logic when dealing with nil

**URL:** <https://www.ruby-forum.com/t/conditional-logic-when-dealing-with-nil/109695>\
**Category:** Rails\
**Created:** [July 18, 2007, 12:39am UTC](https://www.ruby-forum.com/t/conditional-logic-when-dealing-with-nil/109695 "2007-07-18T00:39:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 18, 2007, 12:39am UTC](https://www.ruby-forum.com/t/conditional-logic-when-dealing-with-nil/109695/1 "2007-07-18T00:39:46Z")

</div>

I had a piece of code in an rjs file that checked to see if a variable  
existed:

if @product  
… do something here …  
end

that worked fine, but then I also need to check and see if a column is  
empty or nil, so i changed it to this:

if @product || !@product.price.blank?  
… do something here …  
end

this always gives me “You have a nil object when you didn’t expect  
it!” errors

is there a better way of doing this without nesting if statements  
inside eachother?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 18, 2007, 1:14am UTC](https://www.ruby-forum.com/t/conditional-logic-when-dealing-with-nil/109695/2 "2007-07-18T01:14:50Z")

</div>

Try this:  
if @product && @product.price.blank?

# Do something here

end

In the code below, if @product is nil, the first condition is false so  
the second condition is evaluated. @product.price will fail when  
@product is nil.

Chris
