Sunday, October 16, 2011

Re: [Rails] validates_uniqueness_of before ActiveRecord::RecordNotUnique

On 16 October 2011 05:35, Hesham <hesham.amiri@gmail.com> 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@googlegroups.com.
> 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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate