Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:42:13+00:00 2026-05-27T13:42:13+00:00

I have two models merchant and category with a HABM relationship so have a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:42:13+00:00Added an answer on May 27, 2026 at 1:42 pm

    If you want to do such a query you should transform your HABTM in a has_many :trough and create a model for the joining table and query based on that.

    Alternatively, but possibly with worse performance, you could do:

    Merchant
      .select(:id)
      .joins("categories_merchants ON categories_merchants.merchant_id = merchant.id")
      .where("categories_merchants.category_id IN (?)", [1,2,3,4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, user and group. I also have a joining table groups_users.
I have two models store and category . A store can have many categories
I have two Models, Product and Category, and a join table, Categorizations, for the
I have two models: User and Car with the following associations: User has_many Car
I have two models User and Category that have a HABTM association. I would
I have two models, Story and Category. Using the form for Story#new , I'd
I have two models e.g. Book and Author . An author can have many
I have two models: Director, and Film. I want to create a web query
I have two models: Users and Projects. The idea is that Users can follow
I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.