简体   繁体   中英

Rails 3.2.9 and models in subfolders

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

尝试使用:

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

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!

Wrap your class ContextType ... end to module:

module TypeModels
  class ContextType
    # blah blah
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM