so I am loading 100 posts, and know I will need post.author.name, is there a way to eager load just authors.name rather than all of authors'' fields? thx --~--~---------~--~----~------------~-------~--~----~ 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 3, 9:41 am, rubynuby <dear...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> so I am loading 100 posts, and know I will need post.author.name, is > there a way to eager load just authors.name rather than all of > authors'' fields? thxYou can always use find_by_sql to load just what you want. Something like: Posts.find_by_sql("select posts.*, author.name as author_name from posts join authors on post.author_id = author.id") The Post records that are returned will have an @author_name attribute. In general, I think find_by_sql should be avoided, as ActiveRecord should be used. But when there''s a performance issue, you do what you have to do. ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3 Jan 2008, at 19:37, Mark Wilden wrote:> > On Jan 3, 9:41 am, rubynuby <dear...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> so I am loading 100 posts, and know I will need post.author.name, is >> there a way to eager load just authors.name rather than all of >> authors'' fields? thx > > You can always use find_by_sql to load just what you want. > > Something like: > > Posts.find_by_sql("select posts.*, author.name as author_name from > posts join authors on post.author_id = author.id")> > > The Post records that are returned will have an @author_name > attribute. >You can do the same without find by sql : Post.find :all, :joins => :author, :select => ''posts.*, authors.name as author_name''>Fred> In general, I think find_by_sql should be avoided, as ActiveRecord > should be used. But when there''s a performance issue, you do what you > have to do. > > ///ark > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rubynuby wrote:> so I am loading 100 posts, and know I will need post.author.name, is > there a way to eager load just authors.name rather than all of > authors'' fields? thxAs Mark and Fred have pointed out, this can be done with a join and a custom select. However in other cases where you wish to preserve the object hierarchy, and also ensure fields are converted to the correct Ruby type, you can try this plugin/monkey-patch: http://dev.rubyonrails.org/attachment/ticket/7147/init.5.rb Using this you would write: Posts.find(:all, :conditions => ''...'', :include => :author[:name]) -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---