Hesham
(Hesham)
October 16, 2011, 4:35am
1
I have the following class in a Rails 3.1.1 app:
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio
validates_presence_of :username
validates_uniqueness_of :username, :case_sensitive => false
validates_uniqueness_of :email, :case_sensitive => false
and in my migrations:
add_index :users, :email, :unique => true
add_index :users, :username, :unique => true
However, when I try to create two users with duplicate emails or
usernames I get a DB level exception rather than a failing validation:
ActiveRecord::RecordNotUnique in RegistrationsController#create
PGError: ERROR: duplicate key value violates unique constraint
"index_users_on_username"
There are other validations that work perfectly, but why aren't the
uniqueness validations being performed before the DB create?
I have the following class in a Rails 3.1.1 app:
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio
validates_presence_of :username
validates_uniqueness_of :username, :case_sensitive => false
validates_uniqueness_of :email, :case_sensitive => false
and in my migrations:
add_index :users, :email, :unique => true
add_index :users, :username, :unique => true
However, when I try to create two users with duplicate emails or
usernames I get a DB level exception rather than a failing validation:
If you look in the log can you see it doing the query to look for an
existing matching email or username?
Colin
Hesham
(Hesham)
October 16, 2011, 10:27am
4
> I have the following class in a Rails 3.1.1 app:
> class User < ActiveRecord::Base
> attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio
> validates_presence_of :username
> validates_uniqueness_of :username, :case_sensitive => false
> validates_uniqueness_of :email, :case_sensitive => false
> and in my migrations:
> add_index :users, :email, :unique => true
> add_index :users, :username, :unique => true
> However, when I try to create two users with duplicate emails or
> usernames I get a DB level exception rather than a failing validation:
If you look in the log can you see it doing the query to look for an
existing matching email or username?
Colin
No, there is no such query, just an insert of the record which is
producing the exception.
Colin_Law1
(Colin Law)
October 16, 2011, 12:23pm
5
> I have the following class in a Rails 3.1.1 app:
> class User < ActiveRecord::Base
> attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio
> validates_presence_of :username
> validates_uniqueness_of :username, :case_sensitive => false
> validates_uniqueness_of :email, :case_sensitive => false
> and in my migrations:
> add_index :users, :email, :unique => true
> add_index :users, :username, :unique => true
> However, when I try to create two users with duplicate emails or
> usernames I get a DB level exception rather than a failing validation:
If you look in the log can you see it doing the query to look for an
existing matching email or username?
Colin
No, there is no such query, just an insert of the record which is
producing the exception.
Can you post the code that does the save?
Is the model code you have shown above copied/pasted here from the rb file?
It should not make any difference but have you tried removing the case
sensitive parameter?
What happens if you make new records and save from the rails console?
Colin
Hesham
(Hesham)
October 16, 2011, 1:01pm
6
>> > I have the following class in a Rails 3.1.1 app:
>> > class User < ActiveRecord::Base
>> > attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio
>> > validates_presence_of :username
>> > validates_uniqueness_of :username, :case_sensitive => false
>> > validates_uniqueness_of :email, :case_sensitive => false
>> > and in my migrations:
>> > add_index :users, :email, :unique => true
>> > add_index :users, :username, :unique => true
>> > However, when I try to create two users with duplicate emails or
>> > usernames I get a DB level exception rather than a failing validation:
>> If you look in the log can you see it doing the query to look for an
>> existing matching email or username?
>> Colin
> No, there is no such query, just an insert of the record which is
> producing the exception.
Can you post the code that does the save?
The create method is actually in the registration controller of
Devise.
Is the model code you have shown above copied/pasted here from the rb file?
Yes
It should not make any difference but have you tried removing the case
sensitive parameter?
No difference
What happens if you make new records and save from the rails console?
Let mw check
Hesham
(Hesham)
October 16, 2011, 1:02pm
7
OK - I'm using friendly_id and it seems that it breaks validation in
3.1.1 according to this:
https://github.com/norman/friendly_id/issues/152
Thanks Colin for taking the time