Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-pluginsruby-on-rails-3.1

Vanity gem is throwing an error on my User model


I followed the instructions for the gem perfectly - https://github.com/jaustinhughey/vanities - and everything else worked fine.

But when I tried to load my main page, I am getting this error:

NameError in HomeController#index

undefined local variable or method `has_vanity' for #<Class:0x000001015691e8>
app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/home_controller.rb:3:in `index'

Here is my User.rb

class User < ActiveRecord::Base
        has_vanity

        has_many :feedbacks_as_poster, :foreign_key => :poster_id, :class_name => 'Feedback'
      has_many :feedbacks_as_receiver, :foreign_key => :receiver_id, :class_name => 'Feedback'


end
# == Schema Information
#
# Table name: users
#
#  id         :integer         not null, primary key
#  email      :string(255)
#  f_name     :string(255)
#  l_name     :string(255)
#  username   :string(255)
#  role_id    :integer
#  picture    :string(255)
#  about_me   :string(255)
#  website    :string(255)
#  created_at :datetime
#  updated_at :datetime
#

And here is my Home Controller

class HomeController < ApplicationController
  def index
        @users = User.all
        @feedback = Feedback.new
  end

end

Thoughts?

Edit 1: See my Gemfile below:

source 'http://rubygems.org'

gem 'rails', '3.1.0'

gem 'execjs'
gem 'therubyracer'
gem 'vanities'

group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'    

group :development do
    gem 'sqlite3'
    gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
end

group :test do
  gem 'turn', :require => false
end

group :production do    
    gem 'pg', :require => 'pg'
end

Edit 2: If I go to the URL profile of a user I have created, e.g. localhost:3000/test I see the following error, which leads me to believe that part of the gem is working because it actually fetches the correct record. But something else is broken.

Started GET "/test" for 127.0.0.1 at 2011-09-07 20:00:19 -0500
  Processing by VanitiesController#show as HTML
  Parameters: {"vname"=>"test"}
  Vanity Load (0.2ms)  SELECT "vanities".* FROM "vanities" WHERE "vanities"."name" = 'test' LIMIT 1
Completed 500 Internal Server Error in 100ms

NameError (undefined local variable or method `has_vanity' for #<Class:0x0000010313d478>):
  app/models/user.rb:2:in `<class:User>'
  app/models/user.rb:1:in `<top (required)>'
  app/controllers/vanities_controller.rb:8:in `show'

Solution

  • I've tried to duplicate this problem on my end, and I can't seem to get it to duplicate, meaning it's possibly something unique to your application that may be causing the issue.

    You can reference https://github.com/jaustinhughey/vtest for a working example.

    What I find interesting about your problem is this:

    undefined local variable or method `has_vanity' for #<Class:0x000001015691e8>

    Shouldn't that be "User", not "Class"? Vanities works by pushing up the polymorphic association for an object and a vanity into ActiveRecord as an available method. Then you simply call "has_vanity" to automatically have that polymorphic association available for you. It's a simplistic way to get it in there - only a shortcut, really.

    I'm wondering if there's something else "funky" that's being done to/with ActiveRecord in your case. I'm not sure what specifically would cause that, but for some reason that method just plain doesn't exist for a User object in your case.

    As a work-around, you may try adding the following code in place of the call to "has_vanity":

    class User < ActiveRecord::Base
      has_one :vanity, :as => :vain # instead of has_vanity
    
      has_many :feedbacks_as_poster, :foreign_key => :poster_id, :class_name => 'Feedback'
      has_many :feedbacks_as_receiver, :foreign_key => :receiver_id, :class_name => 'Feedback'
    end