私のデータベーススキーマ:
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])