多态关联应该起作用。您可能需要稍微调整一下才能使其正确,但是通过使用它们,团队课程不必相似,除了有游戏。
module Team
extend ActiveSupport::Concern
included do
has_many :home_games, :class_name => "Game", :as => :team_1
has_many :away_games, :class_name => "Game", :as => :team_2
end
end
class MajorTeam < ActiveRecord::Base
include Team
end
class MinorTeam < ActiveRecord::Base
include Team
end
class Game < ActiveRecord::Base
belongs_to :team_1, :polymorphic => true
belongs_to :team_2, :polymorphic => true
end
我假设你的意思是你has_one
的意思是belongs_to
,has_one
这意味着每支球队只属于一场比赛,这似乎是不正确的。如果我错了,请告诉我。