Rails 6 uniqueness validations

Viewed 36

Currently having trouble with the validate_uniqueness_of method on my model with a given scope.

Right now I have 3 tables: User, College, UsersColleges (join between user and college)

User has_many :users_colleges
College has_many :users_colleges
UsersColleges belongs_to :user, :college

Right now, I am validating the uniqueness of the user_colleges, so that a user can only have 1 entry of the specified college.

validates_uniqueness_of :college_id, scope: :user_id

However, when updating my user with specific colleges, the validation fails -- College has been taken. This behavior only happens for specific colleges, and I can't seem to figure out why.

1 Answers

the syntax for Rails6 would be:

validates :college_id, uniqueness: {scope: :user_id}

See here

I don't know if the older syntax is supported.

Related