This is for Rails 5 / Active Record 5 using PostgreSQL.
Let's say I have two models: Product and Widget. A Product has_many widgets and a Widget has_many products via a join table called products_widgets.
I want to write a query that finds all products that are associated with both the widget with id=37 and the widget with id=42.
I actually have a list of ids, but if I can write the above query, I can solve this problem in general.
Note that the easier version of this query is to find all widgets that are associated with either the widget with id=37 or the widget with id=42, which you could write as follows:
Product.joins(:products_widgets).where(products_widgets: {widget_id: [37, 42]})
But that isn't what I need.