7
我有一个团队模型和Fixtures模型。 Fixtures模型有一个客队和一个主队。我遵循this answer的例子,并且有大部分的工作。Rails has_many自定义ActiveRecord协会
class Fixture < ActiveRecord::Base
belongs_to :home, class_name: 'Team'
belongs_to :away, class_name: 'Team'
end
class Team < ActiveRecord::Base
has_many :home_games, :class_name => 'Fixture', :foreign_key => 'home_id'
has_many :away_games, :class_name => 'Fixture', :foreign_key => 'away_id'
end
我希望能够调用@ team.fixtures得到所有车队灯具的名单,目前@ team.home_games给我的家庭灯具和@ team.away_games给我的跳投。 我该怎么写has_many :games
类似于has_many :home_games
,这是做到这一点的最好方法吗?
看起来不错,谢谢! –