# Question about a rails behaviour

**URL:** <https://www.ruby-forum.com/t/question-about-a-rails-behaviour/143909>\
**Category:** Rails\
**Created:** [July 23, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/question-about-a-rails-behaviour/143909 "2008-07-23T19:04:55Z")\
**Posts on this page:** 2\
**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 23, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/question-about-a-rails-behaviour/143909/1 "2008-07-23T19:04:55Z")

</div>

Hello, my doubt is the following. The line of code below fetches all  
the comments made for an specific event:

```auto
@comments = @event.event_comments.paginate(:all, :include =>
[:comment_by], :order => "created_at DESC", :page =>
params[:page], :per_page => 10)
```

As you can see it has a :include =\> [:comment\_by] to include the name  
of the user who made the comment.

event\_comment model

```auto
belongs_to :comment_by, :select => "id, name, lastname", :foreign_key
=> :commentator_id, :class_name => "User"
```

On my views I have a partial that loops the comments and contains code  
similar to this:

```auto
comment.comment_by.name
```

So my question is:

If I don’t put the include on the find method and let the views as  
they are, does rails make a new query when it sees something like  
comment.comment\_by.name. And if that is the case, if I put the include  
on the find, am I saving those queries from happening for each comment  
later on?

Thanks,

Elioncho

---

<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, 4:16am UTC](https://www.ruby-forum.com/t/question-about-a-rails-behaviour/143909/2 "2008-07-24T04:16:39Z")

</div>

On Wed, Jul 23, 2008 at 12:04 PM, elioncho [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> As you can see it has a :include =\> [:comment\_by] to include the name
> 
> ```auto
> comment.comment_by.name
> ```
> 
> So my question is:
> 
> If I don’t put the include on the find method and let the views as  
> they are, does rails make a new query when it sees something like  
> comment.comment\_by.name.

Yes. Rails will automatically query the database if you request an  
association that is not already loaded.

And if that is the case, if I put the include

> on the find, am I saving those queries from happening for each comment  
> later on?

Yes. You can also verify what SQL queries rails is executing by  
looking in the log files.
