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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Harun Pathan
2011-Oct-16 05:15 UTC
Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
Following links would help you in this http://railswarts.blogspot.com/2007/11/validatesuniquenessof-is-broken-and.html http://drawohara.com/post/18926188/rails-activerecord-validations-are-fatally-flawed I would suggest you to use rails 3 validators instead of earlier 1. Thanks, Harun On Sun, Oct 16, 2011 at 12:35 AM, Hesham <hesham.amiri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Thanks, Harun -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-16 07:49 UTC
Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
On 16 October 2011 05:35, Hesham <hesham.amiri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> > 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? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hesham
2011-Oct-16 10:27 UTC
Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
On Oct 16, 11:49 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 16 October 2011 05:35, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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? > > ColinNo, there is no such query, just an insert of the record which is producing the exception.> > > > > 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? > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > gplus.to/clanlaw-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-16 12:23 UTC
Re: Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
On 16 October 2011 11:27, Hesham <hesham.amiri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Oct 16, 11:49 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 16 October 2011 05:35, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> > 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> >> >> >> >> > 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? >> >> > -- >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. >> >> -- >> gplus.to/clanlaw > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hesham
2011-Oct-16 13:01 UTC
Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
On Oct 16, 4:23 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 16 October 2011 11:27, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > On Oct 16, 11:49 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 16 October 2011 05:35, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > 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> > Colin > > > > > > > > > > > > >> > 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? > > >> > -- > >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > >> > To post to this group, send email to rubyonrails-talk@googlegroups.com. > >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > >> -- > >> gplus.to/clanlaw > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > gplus.to/clanlaw-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hesham
2011-Oct-16 13:02 UTC
Re: validates_uniqueness_of before ActiveRecord::RecordNotUnique
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 On Oct 16, 4:23 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 16 October 2011 11:27, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > On Oct 16, 11:49 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 16 October 2011 05:35, Hesham <hesham.am...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > 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 > > > > > > > > > > > > >> > 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? > > >> > -- > >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > >> > To post to this group, send email to rubyonrails-talk@googlegroups.com. > >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > >> -- > >> gplus.to/clanlaw > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > gplus.to/clanlaw-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.