Rails作用域与另一个模型作用域连接



在一种情况下,我需要在4个范围中加入4个范围,3个范围写在模型(a)中,一个写在另一个模型(B)中,如何加入这些范围?

两个模型具有HABTM关系

Model A
  scope 1
  scope 2
  scope 3
  total_scope= scope1.scope2.scope3.scope4
end
Model B
  scope 4
end

你的问题不是很好措辞,但我认为你问的是有关活动记录的合并功能:

class ModelA
  scope :total_scope, -> { scope1.scope2.scope3.joins(:modelb).merge(ModelB.scope4)
end