Forum: Ruby on Rails Deeply nested assocations

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Beeca027396982f7ba3ced79193e5199?d=identicon&s=25 Martin Hawkins (Guest)
on 2009-01-23 18:25
(Received via mailing list)
I am working on a database that has the following associations It's
not my database, so I am stuck with the structure):

class DimTeam < ActiveRecord::Base
  set_primary_key "team_id"
  has_many :work_match_map_teams, :foreign_key => "to_team_id"

class WorkMatchMapTeam < ActiveRecord::Base
  set_primary_key "map_id"
  belongs_to :work_match, :foreign_key => "work_match_id"
  belongs_to :dim_team, :foreign_key => "to_team_id"

class WorkMatch < ActiveRecord::Base
  set_primary_key "work_match_id"
  has_many :work_balls, :foreign_key => "work_match_id"
  has_many :work_match_map_teams, :foreign_key => "work_match_id"

class WorkBall < ActiveRecord::Base
  set_primary_key "work_ball_id"
  belongs_to :work_match, :foreign_key => "work_match_id"

I can carry out a find as follows:
home_team = DimTeam.find(@search.home_team_id, :include =>
[:work_match_map_teams,   {:work_match_map_teams => :work_match },
{:work_match_map_teams => {:work_match => :work_balls}}])
and while this takes a while, it works.
I'd like to be able to do something like
but this breaks down because home_team.work_match_map_teams is an

I've tried
 team1_matches = {|m|
m.work_match}  but that *is* slow, as it makes a SQL call every time
map is invoked and I still haven't got the required result!

Can anybody please suggest a way in which I can achieve the above with
an efficient SQL call?
This topic is locked and can not be replied to.