# Trying to replace some .find calls with better code

**URL:** <https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880>\
**Category:** Rails\
**Created:** [October 14, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880 "2008-10-14T15:16:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [October 14, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880/1 "2008-10-14T15:16:49Z")

</div>

Hi all,

This forum has been very helpful in the past so I figured I’d try and  
ask for some help on some (yet again) rather “newbie” stuff again.

I currently have the following DB Schema (Two Tables Listed here):

## Users Table (user has\_many children)

id  
login  
…

## Children Table (children belongs\_to user)

id  
first\_name  
last\_name  
is\_lost (boolean)

Users Controller show method:

def show  
@user = User.find\_by\_login(params[:id])

@lost\_children = @user.children.find(:all, :conditions =\> “is\_lost =  
‘t’”)

…  
end

This WORKS. However, I’m sure I can cut down doing a find query by doing  
something like this:

@lost\_children = @user.children(:is\_lost =\> true)

The above doesn’t work for me (as I just made it up out of the blue).  
But there must be something of this nature that can aid in avoiding  
making another DB call. I’ve tried searching for a while now but have  
had little luck.

At the very least, any suggestions on how to make that code  
cleaner/better?

Many thanks in advance!

-Tony

P.S. Please let me know if you need more code or explanations.

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [October 14, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880/2 "2008-10-14T15:34:51Z")

</div>

How about looking into named\_scope.

in your Child model:

named\_scope :lost,  
:conditions =\> “is\_lost = ‘t’”

You can then do this:

@user = User.find\_by\_login(params[:id])  
@lost\_children = @user.children.lost

/franz

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [October 14, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880/3 "2008-10-14T15:36:37Z")

</div>

Forgot to mention that named\_scope is native Rails 2.1 or  
later.

If you’re using an earlier version, you’ll need to install the  
has\_finder plugin.

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [October 14, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/trying-to-replace-some-find-calls-with-better-code/150880/4 "2008-10-14T15:41:53Z")

</div>

Try:

@lost\_children = @user.children.select {|c| c.is\_lost}

-Roy
