Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails scope join with another model scope? [closed]

In a situation i need to join the 4 scopes among the 4 scopes 3 are written in model(A) and one in another model(B) how to join those scopes?

those two models have HABTM relationship

Model A
  scope 1
  scope 2
  scope 3

  total_scope= scope1.scope2.scope3.scope4
end

Model B
  scope 4
end
like image 420
kannathasan Avatar asked Oct 19 '25 10:10

kannathasan


1 Answers

Your question isn't very well worded, but I think what you're asking is related to Active Record's merge feature:

class ModelA
  scope :total_scope, -> { scope1.scope2.scope3.joins(:modelb).merge(ModelB.scope4)
end
like image 65
Peter Brown Avatar answered Oct 21 '25 23:10

Peter Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!