Question

I have a User model that has among other things an email and a university_id

I have added the following line to my model file:

validates_uniqueness_of :email, :scope => [:university_id]

but when I try to create a user with a same email but a different university_id I get the following error message:

ActiveRecord::RecordNotUnique in UsersController#create

SQLite3::ConstraintException: column email is not unique: INSERT INTO "users" ("name", "email", "password", "created_at", "updated_at", "university_id") VALUES ('my name', 'myusername', 'asdfgh', '2012-08-12 04:31:39.135115', '2012-08-12 04:31:39.135115', 2)

I know email is not unique, but the pair email, university_id is, so why am I getting this exception and how can I fix it?

Thanks!

Était-ce utile?

La solution

If you have a uniqueness constraint in your db, it can conflict with ActiveRecord's uniqueness constraint. Check your migrations and remove the constraint if it exists.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top