[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

trying to fo this right

Helder Oliveira

1/9/2007 10:28:00 AM

if
i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db).nil?
miss = 'nemhuma inserida'
else
miss = _
end

i know the _ isnt right, but im trying not to make the query 2 times,
how can i make that if not doing the query two times ?

--
Posted via http://www.ruby-....

2 Answers

Jano Svitok

1/9/2007 11:27:00 AM

0

On 1/9/07, Helder Oliveira <hrpof@sapo.pt> wrote:
> i know the _ isnt right, but im trying not to make the query 2 times,
> how can i make that if not doing the query two times ?

either

ret = i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db)
if ret.nil?
miss = 'nemhuma inserida'
else
miss = ret
end

or (ugly)

if (ret = i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db)).nil?
miss = 'nemhuma inserida'
else
miss = ret
end

NB you can rewrite the first one as:

ret = i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db)
miss = ret.nil? ? 'nemhuma inserida' : ret

or if the result is some object (not false) then you can do

ret = i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db)
miss = ret || 'nemhuma inserida'

or even
miss = i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').inserida.to_s(:db)
|| 'nemhuma inserida'

(if the result is not nil, then only the first term will be evaluated,
otherwise also the second will be).

----
Another issue: I suspect you'll have problem when find() returns nil
(i.e. nothing's found) and you'll be calling nil.inserida.to_s(:db). I
suppose you'd check that nil as well.

i.timesheets.find(:first,:select=>'inserida',:order=>'inseridaDESC').*possible
nil HERE*inserida.to_s(:db)

Helder Oliveira

1/9/2007 11:39:00 PM

0

thanks man ;)

--
Posted via http://www.ruby-....