I have two models merchant and category with a HABM relationship so have a joining table called categories_merchants
How can I write the following query in ActiveRecords?
SELECT
categories_merchants.merchant_id
WHERE
categories_merchants.category_id IN (1,2,3,4)
NOTE: The joining table doesn’t have a model, it works automatically in Rails 3 without need for one.
If you want to do such a query you should transform your HABTM in a
has_many :troughand create a model for the joining table and query based on that.Alternatively, but possibly with worse performance, you could do: