0

这是这个问题的一个轻微的扩展Rails的模型:Rails model that has both 'has_one' and 'has_many' but with some contraints既包括的has_many和HAS_ONE关系,与两种模式有一个的has_many到HAS_MANY关系

在这里,我想涉及两个型号,每个的has_many的其他 - >我有一个在存储外键的模型之间,允许“通过”关系。具体来说,我想涉及的对决和团队,我想每个队“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

这将是一个更好的形式给出,如果你使用的方法,而不是关联的检索current_matchup:

型号:

def current_matchup 
    matchups. where(is_current: true) 
end 
+0

好吧,我很乐意这样做 - 我可以使用该方法在eager_load声明?例如,user.teams.includes(:current_matchup)或类似的东西? –

+0

不幸的是,它不起作用,它必须是一个模型关系。 – Iob

+0

你在尝试中遇到什么错误? – Iob