简体   繁体   English

Rails 3.2.9和子文件夹中的模型

[英]Rails 3.2.9 and models in subfolders

Since rails 3.2.9 I'm unable to store models in subfolders. 自rails 3.2.9以来,我无法在子文件夹中存储模型。 In my app I have this tree: 在我的应用程序中,我有这棵树:

models
 -type_models
 -assets
 -user
 -concerns

Also in application.rb there is 也在application.rb中

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

All things was ok till rails 3.2.9. 所有东西都没问题,直到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: file context_type.rb:

class ContextType ... end

尝试使用:

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

in config/application.rb : 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 : models/type_models/context_type.rb

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

Restart Rails and you're all set! 重新启动Rails,你们都已经完成了!

Wrap your class ContextType ... end to module: 包装你的class ContextType ... end模块:

module TypeModels
  class ContextType
    # blah blah
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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