My setup:
class Team has_many :schedules end
class Schedule belongs_to :team end
Teams Controller:
@team = Team.find(params[:id]) @team_id = params[:id] @schedule = Schedule.list(@team_id)
Schedule Model:
def self.list(teamid) named_scope :team_schedule, lambda { { :conditions => ['team_id = ?', teamid] } } team_schedule :joins => :team, :order => :date_scheduled end
With my list method everything works great and as I want it too. My data returns:
@team.name returns the team name @schedule.opponent returns the team's opponent @schedule.location returns the team's location (home/away/neutral) @schedule.date_played returns the team's scheduled date
Great so far...
Now I want to retrieve the team_id for the (opponent) inside the schedules table. Each team plays approx. 12 opponents. So, I would like to use an each statement to retrieve that bit of data at the same time I'm iterating through my table view...
<% @schedule.each do |schedule| %> <%=h schedule.opponent %> <%=h schedule.opponent.team_id %> <% end %>
.. I'm uncertain how to retrieve the team_id for the opponent listed in the schedules table. team_id is the foreign key for the teams table.
Any help on how to write out this query would be appreciated.