是否有任何方法将来自不同模型的命名范围嵌套在彼此之内?Ruby on Rails:嵌套命名示波器
例子:
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)
或者是我有必要在公司定义另一个named_scope:
:with_employees_and_spouse, :include => {:employees => :spouse}
在这个人为的例子,这不是太糟糕,但嵌套在我的应用更加深入,我想,如果我没有到dd un-DRY代码重新定义嵌套的每个级别的include。
据我所知Rails3中发现者http://m.onkey.org/2010/1/22/active-record-query-interface在过滤器链接方面有改进。 – clyfe 2010-03-05 22:11:25