我的数据库架构:
tournaments(id, ...)
teams(tournament_id, id, ...)
matches(tournament_id, id, team_id_home, team_id_away, ...)
楷模:
class Tournament < ActiveRecord::Base
has_many :teams, dependent: :destroy
has_many :matches, dependent: :destroy
...
end
class Team < ActiveRecord::Base
belongs_to :tournament
...
end
class Match < ActiveRecord::Base
belongs_to :tournament
has_many :teams
...
end
我想在我的视图中有以下数据:
match_id team_id_home team_id_away team_id_home_name team_id_away_name
因此,我正在就以下查询寻求帮助(我正在尝试获取团队的名称,但在加入时遇到问题):
@matches = @tournament.matches.where(:tournament => @tournament).joins(:teams).paginate(page: params[:page])