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

  • Home
  • SEARCH
  • 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 3394840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:13:05+00:00 2026-05-18T04:13:05+00:00

noob question here. I have a has_many :through relationship between items and tags. Finding

  • 0

noob question here. I have a has_many :through relationship between items and tags. Finding all items associated with a given tag is simple enough:

things=Tag.find(1).items

But what if I want to find all items associated with more than one given tag? I was thinking something like:

things=Tag.find(1).items
Tag.find(2).things # wrong, but you get the idea
  • 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-18T04:13:06+00:00Added an answer on May 18, 2026 at 4:13 am

    You can use the array union operator.

    things = Tag.find(1).items | Tag.find(2).items
    

    That will create an object for every item for both tags, which could be way too much depending on what you’re trying to do. If you want something a little more scalable, you can do the lookup on the join table.

    things = ItemTags.find_by_sql("
      SELECT item_id, COUNT(tag_id) AS tag_count 
      FROM item_tags
      WHERE tag_id IN (1, 2)
      GROUP_BY item_id
      HAVING tag_count = 2;
    ").map(&:item)
    

    Just wrote that in browser, so it could be completely wrong. Also, there is probably a way to do that with activerecord finders that would be nicer that a find_by_sql.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of a noob question about Windows Setup projects here. I've
a noob question here. I have the following code: - (IBAction)selectExistingPicture { if ([UIImagePickerController
a noob question here. I have a very strange situation that's very puzzling to
Noob Question here. I have created and successfully added an additional Page Layout to
Total OO noob question here. I have these two methods in a class private
a noob question here. I have an app that does number calculations. I support
This might be a noob question, but can't find an answer anywhere. I have
Maybe this is a noob question, i have an NSURL from savePanel, and i
A noob question I'm sure. For example, say I have a program that looks
a noob question here. After reading the docs, I found that saving a model

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.