【发布时间】:2018-04-04 17:16:51
【问题描述】:
我有两个类似的 M:M 关系,我分别处理它们,但我不知道如何在不冲突的情况下让它们工作。
关系是玩家和团队
1) 许多球员“为”许多球队“效力”
2) 许多玩家“是”许多团队的成员
class Player < ActiveRecord::Base
has_many :plays
has_many :members
has_many :teams, through: :plays
has_many :teams, through: :members
end
class Teams < ActiveRecord::Base
has_many :plays
has_many :members
has_many :players, through: :plays
has_many :players, through: :members
end
class Play < ActiveRecord::Base
belongs_to :players
belongs_to :teams
end
class Member < ActiveRecord::Base
belongs_to :players
belongs_to :teams
end
我需要能够找到:
Player.find(21).teams #who he plays for
Player.find(21).teams #who he is a member of
【问题讨论】:
标签: ruby-on-rails activerecord many-to-many associations has-many-through