Rails 3.2.9 and models in subfolders

寵の児 提交于 2019-12-07 06:03:05

问题


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

回答1:


Try to use:

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



回答2:


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!




回答3:


Wrap your class ContextType ... end to module:

module TypeModels
  class ContextType
    # blah blah
  end
end


来源:https://stackoverflow.com/questions/14236742/rails-3-2-9-and-models-in-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!