# Eager loading of Associations

**URL:** <https://www.ruby-forum.com/t/eager-loading-of-associations/143977>\
**Category:** Rails\
**Created:** [July 24, 2008, 10:36am UTC](https://www.ruby-forum.com/t/eager-loading-of-associations/143977 "2008-07-24T10:36:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 24, 2008, 10:36am UTC](https://www.ruby-forum.com/t/eager-loading-of-associations/143977/1 "2008-07-24T10:36:53Z")

</div>

Hello

I have the following code which looks for all the comments on a  
specified blog post and neighborhood.

* * *

## comment\_activity = PostComment.find(:all, :select =\> “post\_comments.\*”, :joins =\> “INNER JOIN posts ON posts.id = post\_comments.post\_id”, :conditions =\> [“posts.neighborhood\_id = ? AND post\_comments.created\_at \>= ?”, @neighborhood.id, 5.days.ago], :order =\> “post\_comments.created\_at DESC”)

It works great and generates the following SQL:

* * *

## SELECT post\_comments.\* FROM post\_comments INNER JOIN posts ON posts.id = post\_comments.post\_id WHERE (posts.neighborhood\_id = 1 AND post\_comments.created\_at \>= ‘2008-07-19 02:50:54’) ORDER BY post\_comments.created\_at DESC

I added next the following code on the post\_comment model to try and  
fetch the users who wrote each post comment. The post\_comments table  
has a user\_id column that references the user who wrote it.

* * *

## belongs\_to :comment\_by, :select =\> “id, name, lastname”, :foreign\_key =\> “user\_id”, :class\_name =\> “User”

I added the line “:include =\> [:comment\_by]” to my query to load the  
user data like this:

* * *

## comment\_activity = PostComment.find(:all, :include =\> [:comment\_by], :select =\> “post\_comments.\*”, :joins =\> “INNER JOIN posts ON posts.id = post\_comments.post\_id”, :conditions =\> [“posts.neighborhood\_id = ? AND post\_comments.created\_at \>= ?”, @neighborhood.id, 5.days.ago], :order =\> “post\_comments.created\_at DESC”)

But then I get the following generated sql query on my log which is  
not what I am thriving to do:

* * *

## SELECT `post_comments`.`id` AS t0\_r0, `post_comments`.`user_id` AS t0\_r1, `post_comments`.`post_id` AS t0\_r2, `post_comments`.`comment` AS t0\_r3, `post_comments`.`created_at` AS t0\_r4, `post_comments`.`updated_at` AS t0\_r5, `users`.`id` AS t1\_r0, `users`.`email` AS t1\_r1, `users`.`crypted_password` AS t1\_r2, `users`.`salt` AS t1\_r3, `users`.`name` AS t1\_r4, `users`.`lastname` AS t1\_r5, `users`.`gender` AS t1\_r6, `users`.`zipcode` AS t1\_r7, `users`.`birthday` AS t1\_r8, `users`.`remember_token` AS t1\_r9, `users`.`remember_token_expires_at` AS t1\_r10, `users`.`activation_code` AS t1\_r11, `users`.`activated_at` AS t1\_r12, `users`.`created_at` AS t1\_r13, `users`.`updated_at` AS t1\_r14, `users`.`neighborhood_id` AS t1\_r15, `users`.`photo_file_name` AS t1\_r16, `users`.`photo_content_type` AS t1\_r17, `users`.`photo_file_size` AS t1\_r18, `users`.`status_message` AS t1\_r19, `users`.`status_update` AS t1\_r20 FROM post\_comments LEFT OUTER JOIN `users` ON `users`.id = `post_comments`.user\_id INNER JOIN posts ON posts.id = post\_comments.post\_id WHERE (posts.neighborhood\_id = 1 AND post\_comments.created\_at \>= ‘2008-07-19 02:52:10’) ORDER BY post\_comments.created\_at DESC

What do I am doing wrong? Any help or advice? Thanks!
