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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 29, 2008, at 10:06 AM, lundie wrote:> 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!Look at the docs for "eager loading" and the use of the :include option. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great, Ill take a look, Thanks for your help! Rob Biedenharn wrote:> On Jan 29, 2008, at 10:06 AM, lundie wrote: > > 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! > > Look at the docs for "eager loading" and the use of the :include option. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---