独特的has_many通过组合

时间:2014-08-20 20:42:11

标签: ruby-on-rails activerecord unique has-many-through

我只是有这个简单的设置:

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"])

如何确保没有其他团队与这些用户合作?

0 个答案:

没有答案