遵循Rails 样式指南:
更喜欢 has_many :through 到 has_and_belongs_to_many。使用 has_many :through允许连接模型上的附加属性和验证
在你的情况下:
class SeasonTeam < ActiveRecord::Base # couldn't find a better name...
belongs_to :team
belongs_to :season
# the validates are not mandatory but with it you make sure this model is always a link between a Season and a Team
validates :team_id, :presence => true
validates :season_id, :presence => true
before_destroy :do_some_magic
#...
end
class Season < ActiveRecord::Base
has_many :teams, :through => :season_teams
end
class Team < ActiveRecord::Base
has_many seasons, :through => :season_teams
end