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. > > bruceRuby is really particular about logical tests, try this.. if @x.nil? then do one thing else do something else end -- Posted via http://www.ruby-forum.com/.
Bruce Balmer wrote:> @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?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
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 On 5-Jan-06, at 10:13 PM, Justin Forder wrote:> Bruce Balmer wrote: > >> @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? > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails