1
votes

I have the following folder structure:

- models/
  - mappings/
    - user.rb
  - mapping.rb
  - service.rb
  - user.rb
  - ...

In mappings/user.rb, I have the following:

module Mappings
  class User < Mapping

  end
end

And mapping.rb is:

class Mapping < ActiveRecord::Base

end

In config/application.rb, I am autoloading the models as follows:

config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/presenters)

Unfortunately, I am getting this load_missing_constant error:

/home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:481:in `load_missing_constant': Unable to autoload constant User, expected /vagrant/app/models/mappings/user.rb to define it (LoadError)

Any help would be greatly appreciated. I'm not sure what I'm missing!

UPDATE:

Full trace:

16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:180:in const_missing' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:in const_get' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:in block in constantize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in inject' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in constantize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:552:in get' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:583:inconstantize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:302:in get' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:80:in to' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:75:in modules' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:92:in routes' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:159:in default_used_route' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:69:in initialize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:in new' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:in add_mapping' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in block in devise_for' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in devise_for' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:93:in block in ' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:in instance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:in eval_block' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:322:in draw' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:1:in' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in load' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:inblock in load' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:in load_dependency' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:inload' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in block in load_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:ineach' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in load_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:16:inreload!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:26:in block in updater' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:in call' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:in execute' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:27:inupdater' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:7:in execute_if_updated' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/finisher.rb:71:in block in ' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:in instance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:in run' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:55:in block in run_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:226:in block in tsort_each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:348:in block (2 levels) in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:427:in each_strongly_connected_component_from' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:347:in block in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in call' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:224:in tsort_each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:205:in tsort_each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:54:in run_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:300:in initialize!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:in public_send' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:in method_missing' 16:53:48 jobs.1 | from /vagrant/config/environment.rb:8:in <top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:inrequire' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in block in require' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:inload_dependency' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in require' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:276:in require_environment!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/runner.rb:52:in <top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:inrequire' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:in require_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:95:in runner' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in run_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands.rb:17:in ' 16:53:48 jobs.1 | from bin/rails:8:in require' 16:53:48 jobs.1 | from bin/rails:8:in'

So it looks like the issue is because I have to classes named User, even though one is namespaced inside a Module.

1
It seems you are trying to call User rather than Mappings::User - it is however pretty weird as your mappings folder is not directly in the autoload path. Anything intersting in the initalizers?BroiSatse
I am getting this error when starting the rails server, so I'm not actually getting to the point of calling Mappings::User. I also tried adding ` config.autoload_paths += Dir[Rails.root.join('app', 'models', 'mappings','{**}')]` but no success.Adam
in that case couple of things you might try. Firstly, add require: falsenext to a FactoryGirl gem in you Gemfile. Requiring it in a development causes all the models to be loaded on startup (factories are loaded). You should be able to load the console then. Tr to load your models then and find out which is raising this error. Check it for any associations with Mappings::User models which are called :users - most likely you will need to add class_name: 'Mappings::User' to those.BroiSatse
Could you also get a full trace on this error? It will help identify the root cause.Rich Seviora
Added the trace. It looks like the issue is because I have two models named User despite the fact that one is namespaced under Mappings. I figure it will be best to just rename it to UserMapping.Adam

1 Answers

0
votes

I believe it's because you're defining the User class within the Mapping module. It looks at user.rb, doesn't see the User class and errors out. Try removing the module call and see if that works.

Edit: In response to the comment, perhaps you should make that explicit in the class name then. Mappings::User. You'll likely need to inherit it accordingly.