# :all or noop operator for find\_all\_by\_\*?

**URL:** <https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518>\
**Category:** Rails\
**Created:** [July 3, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518 "2009-07-03T21:38:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [July 3, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518/1 "2009-07-03T21:38:34Z")

</div>

Hello,

I’ve been looking for a way to noop a find\_all\_by. Use case: I have a  
function that takes a hash called options as argument, and that  
operates on a subset of Host objects as such:  
“”"  
client\_id = options[:client] ? Client.find(options[:client]) : nil  
old\_hosts = client\_id ?  
Host  
.last\_seen\_before  
(minutes.minutes.ago).find\_all\_by\_client\_id(client\_id) :  
Host.last\_seen\_before(minutes.minutes.ago)  
“”"  
last\_seen\_before is a named\_scope. If I had a noop for the  
find\_all\_by\_client\_id, I could make due of the client\_id ? : part in  
the second line.

I admit that this is quite a simple a case, and that it doesn’t add  
much complexity to the program, but I’ll soon have to add some more  
filters and/or scopes, and I’d hate to make different calls depending  
on whether the option is set or not.

Does anyone know a way to make this less painfull?

Thanks,

Felix

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [July 3, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518/2 "2009-07-03T22:47:20Z")

</div>

Felix SchÃ¤fer wrote:

> Hello,
> 
> I’ve been looking for a way to noop a find\_all\_by.  
> […]  
> Does anyone know a way to make this less painfull?

Hackish, but…

if client\_id.nil?  
finder = :or  
else  
finder = :find\_all\_by\_client\_id  
end

Host.last\_seen\_before.send(finder, client\_id)

> Thanks,
> 
> Felix

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [July 4, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518/3 "2009-07-04T19:44:22Z")

</div>

Assuming that Client has\_many Hosts, you could do:

client\_hosts = options[:client] ? Client.find(options  
[:client]).hosts : Host  
old\_hosts = client\_hosts.last\_seen\_before((minutes.minutes.ago)

–Matt J.

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [July 4, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/all-or-noop-operator-for-find-all-by/171518/4 "2009-07-04T20:43:21Z")

</div>

Am 04.07.2009 um 19:11 schrieb Matt J.:

> Assuming that Client has\_many Hosts, you could do:

Yes, it has.

> client\_hosts = options[:client] ? Client.find(options  
> [:client]).hosts : Host  
> old\_hosts = client\_hosts.last\_seen\_before((minutes.minutes.ago)

That still leaves me with a ? : construct, which in my eyes should be  
useless.

Anyway, thanks Marnen and Matt for the suggestions, but I’ve gone with  
the scope chaining mentioned in another post a little earlier on the  
list. For the record, the result is:  
“”"  
minutes = options[:minutes] || 10  
client\_id = options[:client] ? Client.find(options[:client]) : nil

old\_hosts = Host.last\_seen\_before(minutes.to\_i.minutes.ago)  
old\_hosts = old\_hosts.find\_all\_by\_client\_id(client\_id) unless  
client\_id.nil?  
“”"

For the next options/scopes, I can just continue to do ‘old\_hosts =  
old\_hosts.other\_scope(some\_param) unless some\_param.nil?’ to further  
chain the scopes and thus narrow the list of results as needed.

Regards,

Felix
