Rails: TypeError: wrong argument type Class (expected Module)

krunal shah picture krunal shah · Apr 2, 2014 · Viewed 23.4k times · Source

Inside app/models/abc/xyz.rb

module Abc::Xyz
  extend ActiveSupport::Concern
end

Inside app/models/abc.rb

class Abc < ActiveRecord::Base
  include Abc::Xyz 
end

When I try to fetch data from Abc.where(id: id) sometimes it works and sometimes it returns this error(TypeError: wrong argument type Class (expected Module)).

TypeError: wrong argument type Class (expected Module)
      app/models/abc.rb:2:in `include'
      app/models/abc.rb:2:in `<class:Abc>'
      app/models/abc.rb:1:in `<top (required)>'
      activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `load'
      activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `block in load_file'
      activesupport (3.2.17) lib/active_support/dependencies.rb:639:in `new_constants_in'
      activesupport (3.2.17) lib/active_support/dependencies.rb:468:in `load_file'
      activesupport (3.2.17) lib/active_support/dependencies.rb:353:in `require_or_load'
      activesupport (3.2.17) lib/active_support/dependencies.rb:502:in `load_missing_constant'
      activesupport (3.2.17) lib/active_support/dependencies.rb:192:in `block in const_missing'
      activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `each'
      activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `const_missing'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:230:in `block in constantize'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `each'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `constantize'
      activesupport (3.2.17) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'

Answer

Kostas Rousis picture Kostas Rousis · May 7, 2014

I think that you will be better off sticking to the Rails conventions regarding concerns:

  1. Use modules for defining your concerns
  2. Place concerns within app/models/concerns

If you want to namespace your concern as in Abc::Xyz then make sure that you place it in the right path: app/models/concerns/abc/xyz.rb.

Another fine point is that

module Abc::Xyz

assumes that the module Abc has already been defined. If it hasn't it will fail. So maybe you are better using

class Abc
  module Xyz
    #...
  end
end 

Note that I used class Abc and not module as your code implied. You can't have both a class and a module with the same name. That could be also why you get the occasional errors. Please see this fine article on Exploring Concerns as well.

Rails try various stuff while (auto)loading constants but you are better off sticking to the conventions. I have seen many cases where the same snippets of code fail in different points in time or in different environments due to the order of definitions/loading/execution of constants.