在我的Rails 3.1项目中,我有一些有很多关联的模型。使用ActiveRecord协会的声明,我结束了看起来像这样的模型文件:在Rails 3.1中,我应该如何为具有大量模型的模型格式化ActiveRecord关联声明?
# app/models/some_model.rb
class SomeModel < ActiveRecord::Base
belongs_to :other_model
has_many :more_models
has_many :yet_more_models, :through => :more_models
has_one :another_model, :dependent => :destroy
# ... these declarations continue,
# and continue,
# and continue,
# all the way down to line 32
end
这很快就变成非常丑陋和挫伤我的理解/动机/幸福。我能做些什么来缓解?
- [a]格式/组/以特定方式缩进它们?
- [B]重新思考我的数据模型,因为这可能是设计不良的症状
- [C]与它生活 - 每个人的模型文件这个样子。
你能举一个这些是什么样的资源的例子吗?一个模型中的32个关联看起来很荒唐,我从来没有见过这样的事情! –
[b]如果你有很多关联,你的模型显然有问题。 – Henrik
@AshleyWilliams - 一个这样的资源是'书',它has_many:作者,:语言,:流派,:类别,:主题,:译者,:标签,:标识符,评论...以及其他特定于此应用程序的其他人,以及他们:通过协会。 – GladstoneKeep