如何组织太胖的Rails模型? [英] How to organize Rails models that are too fat?
问题描述
将逻辑从控制器转移到模型中是一种很好的做法.但是在任何复杂的系统中,这总是会导致一个非常大的文件,即使大多数方法都是按照 Rails 方式的一个衬垫.
It is good practice to shift logic from the controller into the model. But in any complex system, this invariably leads to a very large file even if a majority of the methods are one liners as per the Rails Way.
我已经将模型拆分为其他模块并将它们包含在原始模型中,例如,model_flags
、model_validation
等.任何人都有更好的方法?
I've resorted to splitting up models into other modules and including them in the original model, for example, model_flags
, model_validation
, etc. Anyone has a better way?
推荐答案
我意识到这是一个相当古老的问题,它被标记为已回答,但它仍然有很好的 Google 果汁,所以我认为它值得添加...
I realize this is a fairly old question and it's been marked as answered, but it still has good Google juice, so I figured it was worth adding to...
Rails 3 引入了 ActiveSupport::Concern,可用于模块化跨模型共享的行为.或者,就此而言,让已经变得太胖的模特瘦下来.
Rails 3 introduced ActiveSupport::Concern, which can be used to modularize behavior that's shared across models. Or, for that matter, to slim down models that have become too fat.
DHH 自己在这里提供了一个不错的、简洁的示例要点:
DHH himself provides a nice, succinct example gist here:
https://gist.github.com/1014971
这篇关于如何组织太胖的Rails模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!