# Eager loading with find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533>\
**Category:** Rails\
**Created:** [July 4, 2009, 6:59am UTC](https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533 "2009-07-04T06:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dallas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@Dallas](https://www.ruby-forum.com/u/Dallas)\
**Post date:** [July 4, 2009, 6:59am UTC](https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533/1 "2009-07-04T06:59:44Z")

</div>

I have a model Tweets, it has\_many :teams

I wrote a custom find\_by\_sql query for tweets (yes i tried all other  
approaches) such that I can’t eager load the teams. I collect the ids  
and try to fake eager loading myself like so…

team\_hash = {}  
#collect up the tweet ids  
tweet\_ids = tweets.collect{|t| t.id}

#find the associated teams  
TeamTweet.find(:all, :include =\> :team, :conditions =\> “tweet\_id IN (”

- tweet\_ids.join(’,’) +")").each do |tt|  
team\_hash[tt.tweet\_id] = team\_hash[tt.tweet\_id] ? team\_hash  
[tt.tweet\_id] \<\< tt.team : [tt.team]  
end

#reset the teams attribute  
tweets.each do |tweet|  
teams = team\_hash[tweet.id] ? team\_hash[tweet.id] : []  
tweet.write\_attribute(:teams, teams)

# also tried tweet.teams = teams

end

I’m not sure f there is a better way to do this in general but when I  
access tweet.teams it still queries the database for the teams on each  
tweet even though I set it already. I assumed this would be a common  
problem for people using find\_by\_sql but can’t find anything. Any  
solutions or suggestions would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Dallas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@Dallas](https://www.ruby-forum.com/u/Dallas)\
**Post date:** [July 4, 2009, 8:35am UTC](https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533/2 "2009-07-04T08:35:31Z")

</div>

On Jul 4, 12:59 am, Dallas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #find the associated teams
> 
> # also tried tweet.teams = teams
> 
> end

Associations aren’t attributes 🙂 There’s a few bits of state you  
need to fake up to convince AR that it has loaded an association. The  
canonical example of this is probably the stuff in  
association\_preload.rb in Rails itself.

Fred

---

<div class="post-metadata">

**Author:** ![Dallas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@Dallas](https://www.ruby-forum.com/u/Dallas)\
**Post date:** [July 4, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533/3 "2009-07-04T18:57:04Z")

</div>

I’d recommend that you take a look at the association\_preload.rb code  
as well; I also wonder if the list might be able to help with the  
find\_by\_sql query, given that you’re working too hard on the  
conditions below - :conditions =\> { :tweet\_id =\> tweet\_ids } will do  
the same thing you’re spelling out explicitly.

–Matt J.

---

<div class="post-metadata">

**Author:** ![Dallas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@Dallas](https://www.ruby-forum.com/u/Dallas)\
**Post date:** [July 6, 2009, 12:52am UTC](https://www.ruby-forum.com/t/eager-loading-with-find-by-sql/171533/4 "2009-07-06T00:52:47Z")

</div>

Thanks for the tip on the :conditions use Matt. I ended up just  
doing

tweet.write\_attribute(:eager\_teams, teams)

I did this instead of trying to overwrite the association (which  
Frederick aptly pointed out was not an attribute) and access that  
property in the view instead of teams…its not the ideal solution but  
it will do until a better solution presents itself.

Thanks!
