friendly_id icon indicating copy to clipboard operation
friendly_id copied to clipboard

Breaks validates :uniqueness => true

Open the-architect opened this issue 15 years ago • 43 comments

my scenario:

a rails app with a user model which uses friendly_id:

# 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:

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

User.exists?(:email => '[email protected]')

I get the following error:

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?

the-architect avatar Sep 09 '11 19:09 the-architect

I'm having this problem as well - pretty much the same setup.

someoneinomaha avatar Sep 10 '11 21:09 someoneinomaha

+1

recurser avatar Sep 11 '11 06:09 recurser

I also have this problem. The validates_uniqueness_of calls exists?(nil) which is overriden by friendly_id and adds an invalid condition to the query, making the validation always succeed. I think, nil should be considered an "unfriendly_id" in object_utils.rb but I am not sure about other consequences.

Anyway, I made my specs work again by the following monkey patch that I put to config/initializers/friendly_id_patch.rb:

module FriendlyIdPatch

  def friendly_id?
    if [Numeric, Symbol, ActiveRecord::Base, NilClass].detect {|klass| self.class <= klass}
      false
    elsif respond_to?(:to_i) && to_i.to_s != to_s
      true
    end
  end

end

Object.send :include, FriendlyIdPatch

It detects NillClass instances as ids that friendly_id should not handle.

borama avatar Sep 11 '11 08:09 borama

The exists?() implementation also breaks Hash and Array ids :

Person.exists?({:name => 'joe'})
Person.exists?(['name = ?', 'joe'])

recurser avatar Sep 11 '11 09:09 recurser

I've submitted a patch that i think fixes this - if a couple of you guys have time to try it out and confirm would be much appreciated.

https://github.com/norman/friendly_id/pull/153

recurser avatar Sep 11 '11 10:09 recurser

After applying your patch, all my tests worked. Everything seems to be working as expected. Well played!

someoneinomaha avatar Sep 11 '11 13:09 someoneinomaha

The patch works nicely for me too, thanks a lot!

borama avatar Sep 12 '11 08:09 borama

patch works, thank you guys :)

the-architect avatar Sep 12 '11 16:09 the-architect

Will pull @recurser's patch soon. Thanks everybody for the help and feedback.

norman avatar Sep 13 '11 13:09 norman

I'm still encountering this issue (Rails 3.1.1.rc1 and friendly_id at b5b1870531828aa5e986e3dc57338dc374cd1cc1) with the following model:

class AbstractUser < ActiveRecord::Base
  extend FriendlyId

  set_table_name "users"

  validates_presence_of     :login
  validates_uniqueness_of   :login
  validates_format_of       :login, with: /\A[a-z0-9-]+\z/i # Letters, numbers, dashes

  friendly_id :login
end

This happens to be the base of an STI configuration, but that doesn't seem to be the problem -- other classes are failing as well that are not part of the STI hierarchy.

bensie avatar Sep 21 '11 00:09 bensie

This is happening to me when upgraded my app to Rails 3.1.1 (also happens in 3-1-stable branch)

josepjaume avatar Sep 25 '11 08:09 josepjaume

Hmm. This has been WAY more problematic than I anticipated. I'm contemplating removing the override on exists? - this was something I added only fairly recently and it seems it would be less invasive to leave it out. Thoughts?

norman avatar Sep 30 '11 13:09 norman

Would removing that override prevent passing IDs instead of friendly IDs on a friendly_id column when necessary? User.find(123) vs User.find("joe")

Did you track down the commit that caused the issue?

bensie avatar Sep 30 '11 15:09 bensie

I mean, just removing the override on #exists?. I would leave it on #find. I don't know the specific commit, no. I haven't had time to look into it this week but should be able to spend a few hours on Monday to resolve this and the other open issues.

norman avatar Sep 30 '11 15:09 norman

Got it -- I don't have a problem with removing the override on #exists?, but others may want to chime in.

bensie avatar Sep 30 '11 15:09 bensie

Same issue here, breaks all my validates_uniqueness tests :(

invaino avatar Oct 06 '11 09:10 invaino

It seems that resolving issue #143 introduced #exists? in 485a19e. After removing #exists?, uniqueness validation works again in Rails 3.1.1.

znakaska avatar Oct 08 '11 07:10 znakaska

+1 for removing the overrive on #exists? to make it work with Rails 3.1.1.

sush avatar Oct 08 '11 14:10 sush

+1

lankz avatar Oct 10 '11 06:10 lankz

+1

With Rails 3.1.1 the uniqueness validation is still broken

oriolgual avatar Oct 10 '11 08:10 oriolgual