# What am I doing wrong!

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190>\
**Category:** Rails\
**Created:** [January 4, 2006, 1:00am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190 "2006-01-04T01:00:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 4, 2006, 1:00am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190/1 "2006-01-04T01:00:53Z")

</div>

Hi:

@x = Invoice.find(:first)

if @x  
do one thing  
else  
do another  
end

This ought to work but does not. I think that @x is nil but if I  
test for existance or @x != nil or @x.attribute != nil - my code does  
not respond correctly. What is the value of @x if there are no  
records yet in the invoices table?

Thanks in advance.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 4, 2006, 2:53am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190/2 "2006-01-04T02:53:32Z")

</div>

bruce balmer wrote:

> Hi:
> 
> @x = Invoice.find(:first)
> 
> if @x  
> do one thing  
> else  
> do another  
> end
> 
> This ought to work but does not. I think that @x is nil but if I  
> test for existance or @x != nil or @x.attribute != nil - my code does  
> not respond correctly. What is the value of @x if there are no  
> records yet in the invoices table?
> 
> Thanks in advance.
> 
> bruce

Ruby is really particular about logical tests, try this…

if @x.nil? then  
do one thing  
else  
do something else  
end

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 6, 2006, 8:55am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190/3 "2006-01-06T08:55:33Z")

</div>

Thanks Justin. No I was not familiar with using the script/console in  
this manner and although i am familiar with Irb, I was not aware of  
its application in situations like this. other than that I was right  
on top of things 🙂 I appreciate your help.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [January 6, 2006, 6:16am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/89190/4 "2006-01-06T06:16:19Z")

</div>

Bruce B. wrote:

> for existance or @x != nil or @x.attribute != nil - my code does not  
> respond correctly. What is the value of @x if there are no records yet  
> in the invoices table?

It’s easy to answer this for yourself by running script/console in your  
project:

$ script/console  
Loading development environment

> > invoices = Invoice.find(:all)  
> > =\> []
> 
> > inv = Invoice.find(:first)  
> > =\> nil

I assume you are already familiar with using irb to try out ideas in  
Ruby - it’s essential!

regards

Justin
