我只是有这个简单的设置:
class Team < ActiveRecord::Base
has_many :players
has_many :users, -> { uniq }, through: :players
end
class User < ActiveRecord::Base
has_many :players
has_many :teams, -> { uniq }, through: :players
end
class Player < ActiveRecord::Base
belongs_to :team
belongs_to :user
validates :user_id, :uniqueness => { :scope => :team_id }
end
有了这个,我可以通过调用两次来创建具有相同用户组合的多个团队:
Team.create(user_ids: ["1","2"])
如何确保没有其他团队与这些用户合作?