What am I doing wrong!

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

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

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 :slight_smile: I appreciate your help.

bruce

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

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