# Simple activerecord relationship question

**URL:** <https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244>\
**Category:** Rails\
**Created:** [July 16, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244 "2008-07-16T18:24:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244/1 "2008-07-16T18:24:20Z")

</div>

Hey guys, I have a really simple question about activerecord

Lets say I have a Post model which has a has\_many relationship with a  
comments model. (a post is a blog post with many comments)

So, when I execute this

@posts = Post.find(:all)

does every post object in @posts contain all the comment objects for  
that post?

I know if I do a post.comments I will find them there… but I just  
want to know what goes on when I bring all the posts or a single post,  
if the post object will be created with every single related comment  
object that will take a lot of memory every time I instance one

or are the comment objects instanced when I do a .comments and not  
before?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244/2 "2008-07-16T18:29:36Z")

</div>

They’re only instanced, when you access them with  
@post.comments

But you can eager load them with  
@posts = Post.find(:all, :include =\> [:comments])  
for better performance, if you know you’ll use them anyway

In this case it would get them with an INNER JOIN

btw: You can see the generated SQL in development.log

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244/3 "2008-07-16T18:34:52Z")

</div>

thanks for your reply thorsten,

So I don’t have to worry whenever I find a post that it will bring  
along with it hundreds of comment objects? only whenever I try to  
access them they are queried and instanced?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244/4 "2008-07-16T18:48:20Z")

</div>

Yep,  
take te most simple case  
@post = Post.find(123)  
your SQL for the normal find will look like:  
SELECT ALL \* FROM posts WHERE id=123;  
(in principle, the ALL \* is a bit more complicated for Rails  
internals, but not making it slower)  
In this case a  
@posts.comments.each do |comment|  
…do something with them here…  
end

will give you another query like  
SELECT ALL \* FROM comments WHERE post\_id=123;

But only once i think, if you access comments later in code again,  
Rails knows, that it’s already in memory

If you use include  
@post = Post.find(123, :include =\> [:comments])

the SQL would look like  
SELECT ALL \* FROM posts INNER JOIN comments ON  
post.id=comments.post\_id  
which is of course more efficient if you will use them anyway

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/simple-activerecord-relationship-question/143244/5 "2008-07-16T18:52:26Z")

</div>

Thanks! I had my activerecord basics all wrong, this really helps a  
lot
