无需创建子模块即可将模型构建到子文件夹中的优雅方式 [英] Elegant way to structure models into subfolders without creating submodules

查看:10
本文介绍了无需创建子模块即可将模型构建到子文件夹中的优雅方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 app/models 文件夹中有许多模型.我想稍微清理一下这个文件夹.在子文件夹中移动属于彼此的模型.问题是按照惯例,模型类被命名为相应的模块.

I have numerous models in my app/models folder. I'd like to clean this folder up a little bit. Move models that belong to each other in subfolders. The problem is that by convention the model class is namespaced into an according module.

例如

应用程序/模型/博客/post.rb
应用程序/模型/博客/comment.rb
应用程序/模型/user.rb

app/models/blog/post.rb
app/models/blog/comment.rb
app/models/user.rb

这样:

app/models/blog/post.rb

app/models/blog/post.rb

class Post < ActiveRecord
end

而不是

class Blog::Post < ActiveRecord
end

推荐答案

这是我在 Rails 3 中使用的:

Here is what I used for Rails 3:

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]

此配置告诉 Rails 递归扫描所有 app/models 子文件夹并加载所有找到的模型.不需要命名空间.

This configuration tells Rails to scan all the app/models subfolders recursively and load all found models. No namespacing required.

这篇关于无需创建子模块即可将模型构建到子文件夹中的优雅方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆