Forum: Ruby on Rails What am I doing wrong!!!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-04 01:00
(Received via mailing list)
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 02:53
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
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-01-06 06:16
(Received via mailing list)
Bruce Balmer 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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-06 08:55
(Received via mailing list)
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
This topic is locked and can not be replied to.