# Accessing eagerly loaded children

**URL:** <https://www.ruby-forum.com/t/accessing-eagerly-loaded-children/148141>\
**Category:** Rails\
**Created:** [September 9, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/accessing-eagerly-loaded-children/148141 "2008-09-09T19:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 9, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/accessing-eagerly-loaded-children/148141/1 "2008-09-09T19:19:19Z")

</div>

Hello!

In my controller, I have a model that preloads (or eager loads?) its  
respective child members.

class Parent \< ApplicationController

def show  
@parent = Parent.find(:all, :include =\> :children)  
end  
end

In my view, I need to display a specific child by searching for it in  
@parent.children. Right now I am using ActiveRecord’s find, which  
results in an additional query to the database.

@parent.children.find(:all, :conditions =\> [‘name = ?’, jonas])

So would it be possible to use Enumerable’s find method to search the  
eagerly loaded children instead of using ActiveRecord’s find?

Thanks!  
Mike

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 9, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/accessing-eagerly-loaded-children/148141/2 "2008-09-09T19:24:09Z")

</div>

On 9 Sep 2008, at 18:19, Michael Lavrisha wrote:

> end  
> end
> 
> In my view, I need to display a specific child by searching for it in  
> @parent.children. Right now I am using ActiveRecord’s find, which  
> results in an additional query to the database.
> 
> @parent.children.find(:all, :conditions =\> [‘name = ?’, jonas])
> 
> So would it be possible to use Enumerable’s find method to search the  
> eagerly loaded children instead of using ActiveRecord’s find?

Yes! @parent.children.to\_a.find #\<= this is Enumerable’s find

fred

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 9, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/accessing-eagerly-loaded-children/148141/3 "2008-09-09T19:34:18Z")

</div>

Frederick C. wrote:

> Yes! @parent.children.to\_a.find #\<= this is Enumerable’s find
> 
> fred

Sweet, that worked wonderfully!
