Models
class User < ApplicationRecord
has_many :memberships
has_many :pages, through: :memberships
end
class Page < ApplicationRecord
has_many :memberships
has_many :users, through: :memberships
end
class Membership < ApplicationRecord
belongs_to :user
belongs_to :page
validates :page_id, uniqueness: { scope: :user_id}
end
Am able to create multiple user on pages and pages on user, it's like the validation isn't getting called.
To trigger the validations in a associated model you need to use
validates_associated:This can be a real gotcha as validations on associations are not called when they are created implicitly.
Additionally its a good idea to create a compound database index which prevents possible race conditions:
This ensures on the DB level that no two identical rows can be created.