"Uninitialized constant" error when including a module

Ruby on-RailsRubyRuby on-Rails-3.1

Ruby on-Rails Problem Overview


I am trying to reference an association extension but it errors with:

NameError (uninitialized constant User::ListerExtension):
  app/models/user.rb:2:in `<class:User>'

Here is my implementation:

app/models/user.rb
class User < ActiveRecord::Base
  include ListerExtension
  
  has_and_belongs_to_many :roles, :uniq => true, :extend => Lister
lib/lister.rb
module ListerExtension
  def lister
    self.map(&:to_s).join(', ')
  end
end

I am using Rails v3.1.3.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Andrew Marshall has an excellent point about the auto-load setup (see the question he links for more on that), but also: Because you named your class ListerExtension, Rails will be looking for a file named lister_extension.rb - not lister.rb. It's smart, but it's not that smart.

Hope that helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoderamaView Question on Stackoverflow
Solution 1 - Ruby on-RailsXavier HoltView Answer on Stackoverflow