<分区>
在我需要加入 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
<分区>
在我需要加入 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
最佳答案
您的问题措辞不太好,但我认为您的问题与 Active Record's merge feature 有关。 :
class ModelA
scope :total_scope, -> { scope1.scope2.scope3.joins(:modelb).merge(ModelB.scope4)
end
关于ruby-on-rails - Rails 作用域加入另一个模型作用域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24204415/