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

kigster opened this Issue May 19, 2012 · 11 comments


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:

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

  validates_uniqueness_of :name, :scope => :user

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

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.


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:


kigster commented May 23, 2012

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

  1. Error:
    TypeError: Cannot visit Novelist

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


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