In this post, we will see how to resolve What is the Ruby ActiveRecord equivalent for this SQL?
Question:
What would be an optimized (fast) ActiveRecord equivalent in Ruby:shop_id
is a ruby variable passed to the query/association.
Thank youThis above SQL runs as fast as expected.
I am trying to find a pure ActiveRecord solution to avoid something like this:
Best Answer:
I think you do not need theJOIN
at all. A subquery in the where
should be enough:customer_ids
is only a ActiveRecord::Relation
and that the query is not run immediately. It is only run as subquery when the second query is executed.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com