1

我有一个模型'Match',属于一个名为'Team'的模型。球队有很多比赛。它看起来像这样:

 $class Match < ActiveRecord::Base
 belongs_to :home_team, :class_name => "Team"
 belongs_to :away_team, :class_name => "Team"

 class Team < ActiveRecord::Base
 has_many :matches

在数据库中,我已经在“matches”表中正确设置了两个需要的字段 home_team_id 和 away_team_id。我尝试了多种方法,但每当我在匹配控制器中执行此类操作时:

 @matches = Match.find(:all, :include => :team)
 # Or:
 @matches = Match.where('team.gender = ?', true)

我收到此错误:

Association named 'team' was not found; perhaps you misspelled it?

在我看来,该协会不起作用。有任何想法吗?

4

1 回答 1

1

您尚未添加已添加的任何team关联home_teamaway_team因此在您的代码中包括主队和客队。或创建团队关联

还有一件事,您将如何知道哪个是主队和客队,主队或客队应该需要一个外键,并添加foreign_key您的代码以识别它们。

于 2012-09-13T12:57:01.707 回答