Eager loading with find_by_sql

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]

#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


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!

On Jul 4, 12:59 am, Dallas [email protected] wrote:

#find the associated teams

also tried tweet.teams = teams


Associations aren’t attributes :slight_smile: 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.


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.

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

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.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs