# Question about eager loading associations and finder\_sql

**URL:** <https://www.ruby-forum.com/t/question-about-eager-loading-associations-and-finder-sql/68782>\
**Category:** Rails\
**Created:** [September 15, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/question-about-eager-loading-associations-and-finder-sql/68782 "2006-09-15T12:55:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 15, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/question-about-eager-loading-associations-and-finder-sql/68782/1 "2006-09-15T12:55:42Z")

</div>

I’ve got a situation where i have:

class Foo \< ActiveRecord::Base  
has\_many :bars  
end

class Bar \< ActiveRecord::Base  
belongs\_to :foo  
end

the bars table contains a column for storing cached data, which can  
get quite large and there are occassions when I don’t want to load  
that data when I instantiate an object, so I thought I might do

class Foo \< ActiveRecord::Base  
has\_many :bars  
has\_many :bars\_without\_cache, :class\_name =\> “Bar”  
:finder\_sql = ‘select x, y, z from bars where foo\_id  
= #{self.id}’  
end

only to find that if i eagar load the bars\_without\_cache association,  
I still get the cache data I don’t want. This makes sense in a way as  
the whole point of eager loading is to load everything in one query so  
of course the finder\_sql is not being used. i should have figured  
that.

so I guess I am asking is it possible, without having to resort to  
find\_by\_sql or having to go to n+1 queries, to eager load an  
association but only specific columns in the association?

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 18, 2006, 6:51am UTC](https://www.ruby-forum.com/t/question-about-eager-loading-associations-and-finder-sql/68782/2 "2006-09-18T06:51:04Z")

</div>

Chris H. wrote:

> the bars table contains a column for storing cached data, which can  
> only to find that if i eagar load the bars\_without\_cache association,  
> I still get the cache data I don’t want. This makes sense in a way as  
> the whole point of eager loading is to load everything in one query so  
> of course the finder\_sql is not being used. i should have figured  
> that.
> 
> so I guess I am asking is it possible, without having to resort to  
> find\_by\_sql or having to go to n+1 queries, to eager load an  
> association but only specific columns in the association?

You’ll have to use find\_by\_sql, and even so you’ll have to install  
the AR mod at [http://mrj.bpa.nu/eager\_custom\_sql.rb](http://mrj.bpa.nu/eager_custom_sql.rb) so you can  
eager load the bars.

You can prevent having to repeat yourself by defining a method like:

class Foo \< ActiveRecord::Base

def find\_including\_bars\_without\_cache(options = {})  
sql = ‘…’  
sql \<\< “where #{options[:conditions]}” unless  
options[:conditions].nil?  
find\_by\_sql( sql, :include =\> :bars )  
end

end

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

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 18, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/question-about-eager-loading-associations-and-finder-sql/68782/3 "2006-09-18T15:17:48Z")

</div>

thanks for the info. I’ll look into that.

Chris
