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?
On 16 October 2011 05:35, Hesham [email protected] wrote:
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
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
gplus.to/clanlaw
heshama
October 16, 2011, 12:28pm
3
On Oct 16, 11:49am, Colin L. [email protected] wrote:
and in my migrations:
Colin
No, there is no such query, just an insert of the record which is
producing the exception.
On 16 October 2011 11:27, Hesham [email protected] wrote:
validates_presence_of :username
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
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
gplus.to/clanlaw
OK - I’m using friendly_id and it seems that it breaks validation in
3.1.1 according to this:
opened 07:28PM - 09 Sep 11 UTC
closed 02:59PM - 11 Oct 11 UTC
my scenario:
a rails app with a user model which uses friendly_id:
``` ruby
# … Gemfile:
gem 'rails', '>=3.1.0'
gem "friendly_id", :git => "git://github.com/norman/friendly_id.git"
# app/models/user.rb:
class User < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation
validates :email, :presence => true,
:uniqueness => { :case_sensitive => false }
extend FriendlyId
friendly_id :username, :use => :slugged
end
# factories.rb:
FactoryGirl.define do
factory :user do
factory :bob do
username "Example User"
email "[email protected] "
password "foobar"
password_confirmation "foobar"
end
end
end
# spec/models/user_spec.rb
it "should reject duplicate email addresses" do
user = Factory(:bob)
Factory.build(:bob).should_not be_valid
end
```
without the lines:
``` ruby
extend FriendlyId
friendly_id :username, :use => :slugged
```
the spec passes. So I figured it might have something to do with friendly_id.
In fact when I run this with friendly_id enabled
``` ruby
User.exists?(:email => '[email protected] ')
```
I get the following error:
``` ruby
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: slug.email: SELECT 1 FROM "users" WHERE "slug"."email" = '[email protected] ' LIMIT 1
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/sqlite3-1.3.4/lib/sqlite3/database.rb:91:in `initialize'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/sqlite3-1.3.4/lib/sqlite3/database.rb:91:in `new'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/sqlite3-1.3.4/lib/sqlite3/database.rb:91:in `prepare'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:175:in `block in exec_query'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:171:in `exec_query'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:382:in `select'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_one'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_value'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:197:in `exists?'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/bundler/gems/friendly_id-c03879ac0227/lib/friendly_id/finder_methods.rb:30:in `exists?'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `exists?'
from (irb):1
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
from /Users/work/.rvm/gems/ruby-1.9.2-p290@project/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'>>
```
Is this a known issue or is there an easy way around the problem?
Thanks Colin for taking the time
On Oct 16, 4:23pm, Colin L. [email protected] wrote:
validates_uniqueness_of :username, :case_sensitive => false
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
heshama
October 17, 2011, 10:17am
7
Following links would help you in this
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 [email protected] wrote:
There are other validations that work perfectly, but why aren’t the
–
Thanks,
Harun