如何干燥包含中的关联?

How to DRY up associations in includes?

我有一个 scope 方法,我在其中使用 includes 来急切加载所有必要的关联。但我不确定如何干掉 includes?

中使用的关联

这里是模型中定义的scope

scope :with_associates, (lambda do
    includes(user: :contact,
             appointments: [:customer, templates: :page],
             bundles: appointments: [:customer, templates: :page]])
  end)

如何干燥这个范围?

我从来没有试过让它变干。不管怎样,我会努力的。

def appointments_for_includes
  Hash[appointments: [:customer, templates: :page]]
end

def associations_for_includes
  Hash[user: :contact, bundles: appointments_for_includes].merge(appointments_for_includes)
end

我认为在 DRYing 中 - 你会变得不太清楚。为了重复 appointments: [:customer, templates: :page],DRYing 的成本被原始

的清晰度所抵消