# Eager loading a specific field?

**URL:** <https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913>\
**Category:** Rails\
**Created:** [January 3, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913 "2008-01-03T18:42:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 3, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913/1 "2008-01-03T18:42:29Z")

</div>

so I am loading 100 posts, and know I will need post.author.name, is  
there a way to eager load just authors.name rather than all of  
authors’ fields? thx

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 3, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913/2 "2008-01-03T20:38:36Z")

</div>

On Jan 3, 9:41 am, rubynuby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so I am loading 100 posts, and know I will need post.author.name, is  
> there a way to eager load just authors.name rather than all of  
> authors’ fields? thx

You can always use find\_by\_sql to load just what you want.

Something like:

Posts.find\_by\_sql(“select posts.\*, author.name as author\_name from  
posts join authors on post.author\_id = author.id”)

The Post records that are returned will have an @author\_name  
attribute.

In general, I think find\_by\_sql should be avoided, as ActiveRecord  
should be used. But when there’s a performance issue, you do what you  
have to do.

///ark

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 3, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913/3 "2008-01-03T20:48:38Z")

</div>

On 3 Jan 2008, at 19:37, Mark W. wrote:

> Posts.find\_by\_sql(“select posts.\*, author.name as author\_name from  
> posts join authors on post.author\_id = author.id”)

> The Post records that are returned will have an @author\_name  
> attribute.

You can do the same without find by sql : Post.find :all, :joins  
=\> :author, :select =\> ‘posts.\*, authors.name as author\_name’

> 

Fred

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 4, 2008, 2:44am UTC](https://www.ruby-forum.com/t/eager-loading-a-specific-field/124913/4 "2008-01-04T02:44:03Z")

</div>

rubynuby wrote:

> so I am loading 100 posts, and know I will need post.author.name, is  
> there a way to eager load just authors.name rather than all of  
> authors’ fields? thx

As Mark and Fred have pointed out, this can be done with a join and  
a custom select.

However in other cases where you wish to preserve the object hierarchy,  
and also ensure fields are converted to the correct Ruby type, you can  
try this plugin/monkey-patch:

[http://dev.rubyonrails.org/attachment/ticket/7147/init.5.rb](http://dev.rubyonrails.org/attachment/ticket/7147/init.5.rb)

Using this you would write:

Posts.find(:all, :conditions =\> ‘…’, :include =\> :author[:name])

–  
We develop, watch us RoR, in numbers too big to ignore.
