Trying to fo this right

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 ?

On 1/9/07, Helder O. [email protected] 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)

thanks man :wink: