如何组织过胖的Rails模型? [英] How to organize Rails models that are too fat?
问题描述
优良作法是将逻辑从控制器转移到模型中.但是,在任何复杂的系统中,即使按照Rails Way的大多数方法是一个内衬,这也总是会导致文件很大.
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屋!