-
-
Notifications
You must be signed in to change notification settings - Fork 589
-
-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Breaks validates :uniqueness => true #152
Comments
I'm having this problem as well - pretty much the same setup. |
+1 |
I also have this problem. The validates_uniqueness_of calls Anyway, I made my specs work again by the following monkey patch that I put to 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 |
The Person.exists?({:name => 'joe'})
Person.exists?(['name = ?', 'joe']) |
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. |
After applying your patch, all my tests worked. Everything seems to be working as expected. Well played! |
The patch works nicely for me too, thanks a lot! |
patch works, thank you guys :) |
Will pull @recurser's patch soon. Thanks everybody for the help and feedback. |
I'm still encountering this issue (Rails 3.1.1.rc1 and friendly_id at b5b1870) 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. |
This is happening to me when upgraded my app to Rails 3.1.1 (also happens in 3-1-stable branch) |
Hmm. This has been WAY more problematic than I anticipated. I'm contemplating removing the override on |
Would removing that override prevent passing IDs instead of friendly IDs on a friendly_id column when necessary? Did you track down the commit that caused the issue? |
I mean, just removing the override on |
Got it -- I don't have a problem with removing the override on |
Same issue here, breaks all my validates_uniqueness tests :( |
+1 for removing the overrive on |
+1 |
+1 With Rails 3.1.1 the uniqueness validation is still broken |
I'm trying to add a test for this, but it seems to be working fine. Can somebody please try this with the latest FriendlyId (just release beta 13) and if it fails, provide a stack trace and steps to reproduce, or better, modify the test I just added to make it fail? |
I have no stack trace to give as it fails silently. But I have just ran your new test with a fresh friendly_id (4.0.0.beta13) and it fails :
|
Bizarre, the tests run for me. But I just changed the test to use a column other than the slug base and am able to reproduce the failure now. I'm determined to fix this today one way or another, it's been hard to get time to hack on this for the last few weeks and right now I have a brief window of opportunity. Will post another update soon. |
Thanks a lot for your work :) |
I take it back, it's still working fine for me. |
I've cloned the repo and the tests fails both for the same slugged column and a different column (MRI 1.9.2-p290). Thanks for your work! |
Same here, just tested on a different column than the slugged one and fails too. Weird that you can't reproduce it :s |
I'm trying this in a Rails app rather than my Unit tests now, and getting the errors. Not sure why I should see them in an app but not my tests, but that's an issue for later. Will get a fix in soon. |
Ok, a fix has now been pushed to master. The problem was that the method signature for #exists? changed in this commit to Rails, and FriendlyId needed to be updated accordingly. |
Great ! Will be in the beta13 ? Thanks a lot ! |
I just pushed beta 14 actually, after having done a 13 a few hours ago. This week I'll be adding an improved i18n module and then will do a release candidate. Hopefully I can get the stable release out next week, I just want to be really sure there are no embarrassing showstoppers like this one. |
Looking good now @norman, thanks for the fix! |
Thank you for this fix, everything is working great now. |
Hey guys, I've upgraded to rc2 but this test is still failing: with the message:
Any help would be greatly appreciated :-) |
I don't think this has anything to do with FriendlyId. Try generating a random email with Faker, make sure your database is being reset properly between tests, or consider using a sequence in the email address to ensure its uniqueness. |
Yeah all uniqueness issues have been resolved for awhile now, no new test failures surrounding this issue for me. |
It looks like I've misunderstood something. I'd already set up sequences for the email, that's why I explicitly set the email in the second call to FactoryGirl in the code above. I already tested other models in the same project through 'rails console test' that aren't using friendly_id and found that the FactoryGirl.build :charity call didn't hit the database, but the FactoryGirl.build :user hits the db to insert a user. Furthermore, FactoryGirl.create hits the database with an insert call followed by 2 selects and an update (apologies if this is all obvious). I'm not sure how to determine if Devise is getting in the way here, but I guess the key thing I'm confused about is, why would FactoryGirl.build insert a record in the first place? Thanks for a great gem! |
Hi: I'm having problems with 'validates uniqueness:true' in my Rails application. I see this issue was closed two years ago so I don't know if it's me doing something wrong. I'm using rails 4.0.0-rc1. This is my Gemfile: ruby '2.0.0'
gem 'rails', '4.0.0.rc1'
gem 'friendly_id', github: 'FriendlyId/friendly_id', branch: 'master'
gem 'devise', github: 'plataformatec/devise', branch: 'rails4' This is my User model: class User < ActiveRecord::Base
include PictureProfilable
include FriendlyId
friendly_id :name, use: [:slugged]
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :validatable, :trackable, :confirmable
has_and_belongs_to_many :roles, join_table: :roles_users #Work around for https://github.com/thoughtbot/shoulda/issues/225
has_many :services, :dependent => :destroy
...(some public and private methods) I've written this spec: describe 'email should be unique' do
it {
user = FactoryGirl.create :user
new_user = User.new first_name: 'New User', email: user.email, password: 'password'
new_user.valid?.should be_false
}
end Which only passes if I remove friendly_id from the User model. I've tried other models in my application not using Devise and I have the same problem. From the Rails console, if I try to create a User with an existing email, I see the following: user = user = User.new first_name: 'nuevo', email: 'existing@email.com', password: 'password'
user.valid?
User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("slug" = 'nuevo' OR "slug" LIKE 'nuevo--%'ESCAPE '\') ORDER BY LENGTH("slug") DESC, "slug" DESC LIMIT 1
=> true Apparently there is no checking for the uniqueness of the email. Any idea of what can be wrong, or am I doing wrong?. |
@aalbagarcia we have a rails4 branch with work in progress support for Rails 4: gem 'friendly_id', github: 'norman/friendly_id', branch: 'rails4' |
I've tried both of them. I think the problem is related to a new change in the signature of the exists? function. In ActiveRecord/relation/finder_methods.rb (4.0.0-rc1) it is def exists?(conditions = :none) while in friendly_id/finder_methods.rb is def exists?(id = false) Could it be related? |
@aalbagarcia yeah, that could have something to do with it.. |
Thanks!! problem solved. |
Hello! I have class Event < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
belongs_to :admin
validates_presence_of :name
validates_uniqueness_of :name
end With this code, I can't create 2 records with Any idea if the issue could come back to the gem? I've tried setting |
my scenario:
a rails app with a user model which uses friendly_id:
without the lines:
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
I get the following error:
Is this a known issue or is there an easy way around the problem?
The text was updated successfully, but these errors were encountered: