Ruby on Rails:嵌套命名范围

发布于 2024-08-24 05:24:44 字数 604 浏览 8 评论 0原文

有没有办法将不同模型的命名范围嵌套在一起?

示例:

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 =>; {:员工=> :spouse}

在这个人为的示例中,这还不错,但是我的应用程序中的嵌套更深,如果我不必添加在每个位置重新定义包含的非 DRY 代码,我会喜欢它嵌套的级别。

Is there any way to nest named scopes inside of each other from different models?

Example:

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

Is there any nice way for me to find a company while including employees and spouses like this:
Company.with_employees.with_spouse.find(1)
or is it necessary for me to define another named_scope in Company:
:with_employees_and_spouse, :include => {:employees => :spouse}

In this contrived example, it's not too bad, but the nesting is much deeper in my application, and I'd like it if I didn't have to add un-DRY code redefining the include at each level of the nesting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-08-31 05:24:44

您可以使用默认范围,

class Company
  default_scope :include => :employees
  has_many :employees
end

class Employee
  default_scope :include => :spouse
  belongs_to :company
  belongs_to :spouse
end

class Spouse
  has_one :employee
end

然后这应该可以工作。不过我还没有测试过。

Company.find(1)          # includes => [:employee => :spouse]

You can use default scoping

class Company
  default_scope :include => :employees
  has_many :employees
end

class Employee
  default_scope :include => :spouse
  belongs_to :company
  belongs_to :spouse
end

class Spouse
  has_one :employee
end

Then this should work. I have not tested it though.

Company.find(1)          # includes => [:employee => :spouse]
玩心态 2024-08-31 05:24:44

您需要始终定义所有条件。但是你可以定义一些方法来组合一些named_scope


class Company
  has_many :employees
  named_scope :with_employees, :include => :employees
  named_scope :limit, :lambda{|l| :limit => l }

  def with_employees_with_spouse
    with_employees.with_spouse
  end

  def with_employees_with_spouse_and_limit_by(limit)
    with_employees_with_spouse.limit(limit)
  end

end
class Employee
  belongs_to :company
  belongs_to :spouse
  named_scope :with_spouse, :include => :spouse
end
class Spouse
  has_one :employee
end

You need define all the time all of your conditions. But you can define some method to combine some named_scope


class Company
  has_many :employees
  named_scope :with_employees, :include => :employees
  named_scope :limit, :lambda{|l| :limit => l }

  def with_employees_with_spouse
    with_employees.with_spouse
  end

  def with_employees_with_spouse_and_limit_by(limit)
    with_employees_with_spouse.limit(limit)
  end

end
class Employee
  belongs_to :company
  belongs_to :spouse
  named_scope :with_spouse, :include => :spouse
end
class Spouse
  has_one :employee
end
我的黑色迷你裙 2024-08-31 05:24:44

试试这个

Company.with_employees.merge( Employees.with_spouse)

try this

Company.with_employees.merge( Employees.with_spouse)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文