Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

scoped slugs break with scoped validations: TypeError: Cannot visit User #286

Closed
kigster opened this Issue May 19, 2012 · 11 comments

Comments

Projects
None yet
5 participants

kigster commented May 19, 2012

Using Rails 3.2.3, Ruby 1.9.3.

I created a brand new Rails project to reproduce this bug:
https://github.com/wanelo/friendly_id_scoped_bug

Run "rake test" on the Rails project to reproduce the error.

In a nutshell, scoped slug + scoped validation is not working on the same model.

class Collection < ActiveRecord::Base
  attr_accessible :name, :user, :slug
  belongs_to :user

  # THIS LINE CAUSES TEST FAILURE
  # TOGETHER WITH SCOPED FRIENDLY_ID
  validates_uniqueness_of :name, :scope => :user

  extend FriendlyId
  friendly_id :name, :use => :scoped, :scope => :user
end
Collaborator

parndt commented May 19, 2012

Does it still happen if you move the validator after the FriendlyId definition?

kigster commented May 19, 2012

Yup, sure does.

Collaborator

parndt commented May 19, 2012

Great; thanks for confirming. Do you think it possible to write a test case for FriendlyId itself?

kigster commented May 19, 2012

I could give it a shot over the weekend.

@kigster kigster closed this May 19, 2012

@kigster kigster reopened this May 19, 2012

kigster commented May 19, 2012

Oops sorry wrong button :)

kigster commented May 22, 2012

I am not exactly sure how to create a test within the gem that reproduces this. Create two database tables, create two models, one with nested scope, run the test, and then drop the tables? Is there a better way?

kigster commented May 23, 2012

OK I added a validation to the scoped tests model, and I get 16 test failures:

https://github.com/kigster/friendly_id/commit/20cf2ed84a4b6c72efbc0b2ad912ad40aacc788f

kigster commented May 23, 2012

And by the way, here is the error I was getting in the first place:

  1. Error:
    test_should_not_allow_duplicate_slugs_inside_scope(ScopedTest):
    TypeError: Cannot visit Novelist

I had this (almost) exact same issue. You need to change :scope => :user to be :scope => :user_id

@ghost

ghost commented Jan 25, 2013

Thanks allhailskippy, THAT is the solution I was looking for!

The exception had me looking in all the wrong places.

@norman norman closed this Jan 25, 2013

@allhailskippy and @kailosbryan, same exact problem. So happy I found this thread. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment