I just have this simple setup:
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
With this I can create multiple Teams with the same user combination by calling this twice:
Team.create(user_ids: ["1","2"])
How to make sure that there is not already another team with these users?