扶手:优雅的方式来构建模型到子文​​件夹,而无需创建子模块 [英] Rails: Elegant way to structure models into subfolders without creating submodules

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

问题描述

我有许多车型在我的应用程序/模型文件夹中。我想清理这个文件夹了一点点。此举属于彼此的子型号。的问题是,按照惯例模型类命名空间成根据模块。

例如。

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

这样:

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

 类岗位< ActiveRecord的
结束
 

而不是

 类博客::邮报< ActiveRecord的
结束
 

解决方案

下面是我用Rails的3:

  config.autoload_paths + = DIR [Rails.root.join(应用程序,模型,{} **')]
 

此配置告诉Rails扫描所有的应用程序/模型的子文件夹递归和加载所有发现的模型。没有命名空间必需的。

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.

E.g.

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

so that:

app/models/blog/post.rb

class Post < ActiveRecord
end

and not

class Blog::Post < ActiveRecord
end

解决方案

Here is what I used for Rails 3:

config.autoload_paths += Dir[Rails.root.join('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天全站免登陆