Skip to content
This repository

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

Closed
brodock opened this Issue February 11, 2012 · 5 comments

2 participants

Gabriel Mazetto Andrew White
Gabriel Mazetto

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'
Andrew White
Owner

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'
Gabriel Mazetto

@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.

Andrew White
Owner

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

config.autoload_paths += [File.expand_path("#{config.root}/../base_project/app/models")]
Gabriel Mazetto

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

Andrew White
Owner

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.

Andrew White pixeltrix closed this April 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.