0

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!

marimaf
  • 5,382
  • 3
  • 50
  • 68
  • Check your migration, did you create the column with a uniqueness constraint? If you did, you have to remove it -- it conflicts with the activerecord validation. – Tanzeeb Khalili Aug 12 '12 at 04:53
  • That was it! Thank you. Please add it as an answer so I can accept it :) – marimaf Aug 12 '12 at 05:01

1 Answers1

0

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.

Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27