# Trying to fo this right

**URL:** <https://www.ruby-forum.com/t/trying-to-fo-this-right/81486>\
**Category:** Ruby\
**Created:** [January 9, 2007, 11:28am UTC](https://www.ruby-forum.com/t/trying-to-fo-this-right/81486 "2007-01-09T11:28:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [January 9, 2007, 11:28am UTC](https://www.ruby-forum.com/t/trying-to-fo-this-right/81486/1 "2007-01-09T11:28:12Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [January 9, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/trying-to-fo-this-right/81486/2 "2007-01-09T12:28:14Z")

</div>

On 1/9/07, Helder O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [January 10, 2007, 12:38am UTC](https://www.ruby-forum.com/t/trying-to-fo-this-right/81486/3 "2007-01-10T00:38:28Z")

</div>

thanks man 😉
