0

这是这个问题的轻微扩展:Rails model that has both 'has_one' and 'has_many' but with some constraints

在这里,我试图将两个模型关联起来,每个模型都有另一个 -> 我有一个中间模型,它存储外键,允许“通过”关系。具体来说,我正在尝试将比赛和球队联系起来,我希望每个球队都“has_one :current_matchup”

以下是我的模型的相关摘录:

团队:

has_many :matchup_teams
has_many :matchups, through: :matchup_teams

配对:

has_many :matchup_teams
has_many :teams, through: :matchup_teams

比赛队伍:

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"
4

1 回答 1

0

如果您使用方法而不是关联来检索 current_matchup,那将是一种更好的方法:

型号:团队

def current_matchup
    matchups. where(is_current: true) 
end
于 2017-06-15T16:40:27.107 回答