I was reading in the Agile book how to use the :include option when doing a find(:all,...). It doesn''t seem to fit my scenario. I have an Article object that has many Comments which in turn belong to a User. Right now I''m doing the following controller... @article=Article.find(params[:id]) view... for comment in @article.comments posted by <%=comment.user.username %> <%=comment.body %> end This results in a select for each user as i''m looping through. Is there a way to use the :include method while still using the native objects? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 9/20/05, Matt Pantana <matt.pantana-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I was reading in the Agile book how to use the :include option when doing a > find(:all,...). It doesn''t seem to fit my scenario. I have an Article > object that has many Comments which in turn belong to a User. > > Right now I''m doing the following > > controller... > @article=Article.find(params[:id]) > > view... > for comment in @article.comments > > posted by <%=comment.user.username %> > <%=comment.body %> > end > > This results in a select for each user as i''m looping through. Is there a > way to use the :include method while still using the native objects?You can''t do 3-level includes like that currently. Two options: You can modify the finder_sql for the Article.comments has_many association so it pulls in some user fields too. Rails will piggy-back them on to the comments model without any hassle. Or, you can just do this: Comment.find :all, :conditions => [''article_id = ?'', @article.id], :include => :user -- rick http://techno-weenie.net
couldnt you go @article.comments.find :all, :include => [''user''] resulting in fetching the comments twice over but it beats select each user one at a time. On 9/20/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/20/05, Matt Pantana <matt.pantana-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I was reading in the Agile book how to use the :include option when doing a > > find(:all,...). It doesn''t seem to fit my scenario. I have an Article > > object that has many Comments which in turn belong to a User. > > > > Right now I''m doing the following > > > > controller... > > @article=Article.find(params[:id]) > > > > view... > > for comment in @article.comments > > > > posted by <%=comment.user.username %> > > <%=comment.body %> > > end > > > > This results in a select for each user as i''m looping through. Is there a > > way to use the :include method while still using the native objects? > > You can''t do 3-level includes like that currently. > > Two options: > > You can modify the finder_sql for the Article.comments has_many > association so it pulls in some user fields too. Rails will > piggy-back them on to the comments model without any hassle. Or, you > can just do this: > > Comment.find :all, :conditions => [''article_id = ?'', @article.id], > :include => :user > > > -- > rick > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>