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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 9 Sep 2008, at 18:19, Michael Lavrisha wrote:> > 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?Yes! @parent.children.to_a.find #<= this is Enumerable''s find fred> > > Thanks! > Mike > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> Yes! @parent.children.to_a.find #<= this is Enumerable''s find > > fredSweet, that worked wonderfully! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---