Search code examples
ruby-on-railsdeviseincluderuby-on-rails-5activesupport-concern

Rails 5 enum inside model's Concerns generates NameError: uninitialized constant


Rails 5 here.

I tried without luck to load a concern inside my User model (devise model). The include Levelable works without any problem on other models. Do I have to configure a special config/helper/initializer regarding this special model to make it work?

Model concern Levelable is used by models User and Client

#models/concerns/levelable.rb 
require 'active_support/concern'

module Levelable
  extend ActiveSupport::Concern

  included do
    enum level: { beginner: 0, intermediate: 1, advanced: 2, very_advanced: 3 }
  end
end

And the model (shortened for readability)

#models/user.rb
class User < ApplicationRecord

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

  include Levelable

  acts_as_messageable
end

This setup result is a: NameError: uninitialized constant User::Levelable Same error happens on Client model so I guess something is not working with my concerns load. I already tried to autoload the path inside config/application.rb without any change.

I can not figure out how to make the load of the Concerns works, any help would be great. Thank you !


Solution

  • I had this problem some time ago and after some digging I found out that my concern files where named using the Capitalize form (for some crazy reason).

    So I renamed them from Searchable.rb to searchable.rb and it's all done! :)

    PS. IF you use git/github, the diff isn't case sensitive so if you rename them from Levelable.rb to levelable.rb it won't appear in the git status.

    Cheers