这是此问题的略微扩展:Rails model that has both 'has_one' and 'has_many' but with some contraints
在这里,我试图将两个模型联系起来,每个模型都有另一个模型 - >我有一个介于外键之间的模型,允许一个"通过"关系。具体来说,我试图将比赛和球队联系起来,我希望每支球队能够和#34; has_one:current_matchup"
以下是我模特的相关摘录:
小组:
has_many :matchup_teams
has_many :matchups, through: :matchup_teams
对决:
has_many :matchup_teams
has_many :teams, through: :matchup_teams
MatchupTeam:
belongs_to :matchup
belongs_to :team
我该怎么做? 这是我当前的尝试,导致错误:
模型团队:
has_one :current_matchup_team, -> { where(is_current: true) }, :class_name=> "MatchupTeam"
has_one :current_matchup, through: :current_matchup_team, :class_name=>"Matchup"
答案 0 :(得分:0)
如果您使用方法而不是关联来检索current_matchup,这将是一个更好的方法:
型号:团队
def current_matchup
matchups. where(is_current: true)
end