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

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

Comments

Projects
None yet
2 participants
@brodock

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'
@pixeltrix

This comment has been minimized.

Show comment
Hide comment
@pixeltrix

pixeltrix Feb 12, 2012

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'
Member

pixeltrix commented Feb 12, 2012

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

This comment has been minimized.

Show comment
Hide comment
@brodock

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

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.

@pixeltrix

This comment has been minimized.

Show comment
Hide comment
@pixeltrix

pixeltrix Feb 12, 2012

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")]
Member

pixeltrix commented Feb 12, 2012

@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

This comment has been minimized.

Show comment
Hide comment
@brodock

brodock Feb 12, 2012

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

brodock commented Feb 12, 2012

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

@pixeltrix

This comment has been minimized.

Show comment
Hide comment
@pixeltrix

pixeltrix Apr 29, 2012

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.

Member

pixeltrix commented Apr 29, 2012

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