有没有办法将不同模型的命名范围相互嵌套?
例子:
class Company
has_many :employees
named_scope :with_employees, :include => :employees
end
class Employee
belongs_to :company
belongs_to :spouse
named_scope :with_spouse, :include => :spouse
end
class Spouse
has_one :employee
end
有什么好方法可以让我找到一家公司,同时包括这样的员工和配偶:
Company.with_employees.with_spouse.find(1)
或者我是否有必要在 Company 中定义另一个 named_scope:
:with_employees_and_spouse, :include => {:employees => :spouse}
在这个人为的例子中,它并不算太糟糕,但是我的应用程序中的嵌套要深得多,如果我不必添加 un-DRY 代码来重新定义嵌套的每个级别的包含,我会喜欢它。