# Limiting columns in model association

**URL:** <https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190>\
**Category:** Rails\
**Created:** [March 8, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190 "2007-03-08T20:41:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 8, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/1 "2007-03-08T20:41:33Z")

</div>

So we have this model:

class Post \< ActiveRecord::Base

belongs\_to :person

But when I join @post.person I only want the id and name. The docs  
fleetingly mention using :select to accomplish this but there are no  
examples and no explanations. I tried:

belongs\_to :person, :select =\> [:id, :name]  
belongs\_to :person, :select =\> {:id, :name}

…without success. Could anyone explain how to use the :select  
parameter? Thanks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 8, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/2 "2007-03-08T20:44:56Z")

</div>

On 3/8/07, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> examples and no explanations. I tried:
> 
> >

It’s a string, you’d need to do

belongs\_to :person, :select =\> “id AND name”

Pat

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 9, 2007, 2:39am UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/3 "2007-03-09T02:39:46Z")

</div>

First, :select defines a string that will go directly in to the SELECT

- FROM \* WHERE clause of the SQL statements ActiveRecord uses, so “id  
AND name” will NOT work.

Second, in this particular case you probably shouldn’t be limiting the  
columns on the MODEL level. Instead, defining select for the find  
will do:

```
@posts = Post.find( :all,
                    :select => "id, name",
                    :conditions => "is_deleted = false",
                    :order => "posts.created_at DESC",
                    :include => :person)

```

On Mar 9, 4:24 am, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 8, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/4 "2007-03-08T21:24:51Z")

</div>

Does this not work with :include? Here’s my function:

```
@posts = Post.find( :all,
                    :conditions => "is_deleted = false",
                    :order => "posts.created_at DESC",
                    :include => :person)

```

and the corresponding error:

ArgumentError: Unknown key(s): select

POST.RB  
class Post \< ActiveRecord::Base

belongs\_to :person, :select =\> “id AND name”

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 10, 2007, 4:21am UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/5 "2007-03-10T04:21:50Z")

</div>

Ooh… yeah, it doesn’t. :include screws up the select portion of the  
query. Sorry, I missed that key in the list.

Anyway, is there a reason to limit person to id/name? At this point,  
you can still do it, but you’ll have to use explicit SQL joins.

```
@posts = Post.find( :all,
                    :select => "posts.*, persons.id AS person_id,

```

persons.name AS person\_name",  
:join =\> “INNER JOIN #{Person.table\_name} ON  
persons.id = posts.person\_id”,  
:conditions =\> “is\_deleted = false”,  
:order =\> “posts.created\_at DESC”)

Then your @posts array will have a bunch of Post objects with all  
their attributes plus two new ones – person\_id and person\_name.

Using :include and referencing the Person model off the Post object is  
much cleaner though…

On Mar 10, 3:13 am, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 12, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/6 "2007-03-12T21:31:24Z")

</div>

Eden Li wrote:

> Ooh… yeah, it doesn’t. :include screws up the select portion of the  
> query. Sorry, I missed that key in the list.
> 
> Anyway, is there a reason to limit person to id/name? At this point,  
> you can still do it, but you’ll have to use explicit SQL joins.

The reason was just one of optimization - why load all the person data  
when all I need is the name and id? But its not worth writing nasty SQL  
for every :include!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 9, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/limiting-columns-in-model-association/88190/7 "2007-03-09T20:13:52Z")

</div>

Eden Li wrote:

> First, :select defines a string that will go directly in to the SELECT
> 
> - FROM \* WHERE clause of the SQL statements ActiveRecord uses, so “id  
> AND name” will NOT work.
> 
> Second, in this particular case you probably shouldn’t be limiting the  
> columns on the MODEL level. Instead, defining select for the find  
> will do:
> 
> ```
> @posts = Post.find( :all,
> :select => "id, name",
> :conditions => "is_deleted = false",
> :order => "posts.created_at DESC",
> :include => :person)
> 
> ```

This doesn’t work, unfortunately. Does rails even pass :select with  
include?
