Skip to content

Autoloading models from external folder fails on Rails 3.1, 3.2 #5007

brodock opened this Issue Feb 11, 2012 · 5 comments

2 participants

brodock commented Feb 11, 2012

What I'm basically doing is sharing some models across multiple projects. I have a base project where all models are defined and some other projects that requires thous models, as all of them use the same data.

By that said, what I used to do is define a config.autoload_paths pointing to the other projects model folder:

 config.autoload_paths += %W(#{config.root}/../base_project/app/models)

With Rails 3.0 it works perfectly fine, however with Rails 3.1, 3.2, I get uninitialized constnat to every model name, for example:

 NameError (uninitialized constant ApplicationController::User):
   app/controllers/application_controller.rb:11:in `current_user'
Ruby on Rails member

Does it work if you use File.expand_path?

Also another way of achieving what you want is to create an engine with your shared models and then using a :path spec in your Gemfile, e.g:

gem 'shared_models', :path => '/path/to/shared_models'
brodock commented Feb 12, 2012

@pixeltrix I don't know if I'm doing it wrong but, using File.expand_path gave me:

 can't modify frozen Array

Here is the snipet:

 config.autoload_paths << File.expand_path( "#{config.root}/../base_project/app/models" )

The engine may became an alternative, but I still looking for a way to make Rails look for the shared models without using it.

Ruby on Rails member

@brodock you can't modify autoload_paths in place - use +=, e.g:

config.autoload_paths += [File.expand_path("#{config.root}/../base_project/app/models")]
brodock commented Feb 12, 2012

@pixeltrix Ok, got it right this time, but still same result.

Ruby on Rails member

Okay I can confirm this works for me in 3.2.3 if you can provide any further information on how it's not working for you then I'll take another look at it. You don't need the File.expand_path but it doesn't work using star paths e.g. #"{config.root}/../shared/**/*" won't work.

@pixeltrix pixeltrix closed this Apr 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.