将模型结构化为子文件夹而无需创建子模块的绝佳方法 [英] Elegant way to structure models into subfolders without creating submodules

查看:64
本文介绍了将模型结构化为子文件夹而无需创建子模块的绝佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.

例如

app / models / blog / post.rb

app / models /blog/comment.rb

app / models / 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递归扫描所有应用程序/模型子文件夹,然后加载所有找到的模型。无需命名空间。

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

这篇关于将模型结构化为子文件夹而无需创建子模块的绝佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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