5

Since rails 3.2.9 I'm unable to store models in subfolders. In my app I have this tree:

models
 -type_models
 -assets
 -user
 -concerns

Also in application.rb there is

config.autoload_paths += Dir["#{config.root}/app/models/*"]

All things was ok till rails 3.2.9. Now I have "Unknown constant" error. I don't want to namespace tons of model and fix all app to use namespaced models.

Warning: Error loading /var/www/my_app/app/models/type_models/context_type.rb:
uninitialized constant TypeModels::ContextType

file context_type.rb:

class ContextType ... end
dmon_s
  • 51
  • 4

3 Answers3

0

Try to use:

config.autoload_paths += Dir["#{config.root}/app/models/**/"]
Baruch
  • 1,133
  • 10
  • 18
  • 1
    Warning: Error loading /var/www/my_app/app/models/type_models/context_type.rb: uninitialized constant TypeModels::ContextType – dmon_s Jan 09 '13 at 14:00
  • See: http://stackoverflow.com/questions/18934115/rails-4-organize-rails-models-in-sub-path-without-namespacing-models Did you restart your app after the change? – RubeOnRails Jan 16 '14 at 23:41
0

in config/application.rb:

config.autoload_paths += %W(type_models assets user concerns).map { |folder| "#{config.root}/app/models/#{folder}"}

in models/type_models/context_type.rb:

class TypeModels::ContextType < ActiveRecord::Base
  ...
end

Restart Rails and you're all set!

Yossi Shasho
  • 3,632
  • 31
  • 47
-1

Wrap your class ContextType ... end to module:

module TypeModels
  class ContextType
    # blah blah
  end
end
TLama
  • 75,147
  • 17
  • 214
  • 392
Semjon
  • 983
  • 6
  • 17