【发布时间】:2017-06-15 16:27:36
【问题描述】:
这是这个问题的一个小扩展:Rails model that has both 'has_one' and 'has_many' but with some contraints
在这里,我试图将两个模型关联起来,每个模型都有多个 -> 我有一个中间模型,它存储外键,允许“通过”关系。具体来说,我正在尝试将比赛和球队联系起来,我希望每个球队都“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"
【问题讨论】:
标签: ruby-on-rails activerecord has-many has-one has-one-through