# Find on Element without hitting the database

**URL:** <https://www.ruby-forum.com/t/find-on-element-without-hitting-the-database/127467>\
**Category:** Rails\
**Created:** [January 29, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/find-on-element-without-hitting-the-database/127467 "2008-01-29T16:07:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [January 29, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/find-on-element-without-hitting-the-database/127467/1 "2008-01-29T16:07:13Z")

</div>

Hi,

I have a task model with a one to many relationship with a todo model.

If I do a find on the task model:

task = Tasks.find(1)

Is there a way to search the todos on a particular field without  
hitting the database again. For example, If the todo model had a field  
named label, could I do something like task.todos.find\_by\_label("…")  
but not have it hit the database, because I already have all of the  
todos within the task.

Is it best just to write a helper method that loops through the todos,  
looking for the one with the correct label or is there something  
already build into ruby to do this?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [January 29, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/find-on-element-without-hitting-the-database/127467/2 "2008-01-29T16:19:34Z")

</div>

On Jan 29, 2008, at 10:06 AM, lundie wrote:

> named label, could I do something like task.todos.find\_by\_label(“…”)  
> but not have it hit the database, because I already have all of the  
> todos within the task.
> 
> Is it best just to write a helper method that loops through the todos,  
> looking for the one with the correct label or is there something  
> already build into ruby to do this?
> 
> Thanks for your help!

Look at the docs for “eager loading” and the use of the :include option.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [January 29, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/find-on-element-without-hitting-the-database/127467/3 "2008-01-29T17:05:43Z")

</div>

Great, Ill take a look,

Thanks for your help!
