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
This might be a noob question, but can't find an answer anywhere. I have
I know this may be a noob question, but it's bugging the heck out
This is going to be the noobist of all noob questions, but what exactly
I am a noob when it comes to python. I have a python script
First of all, I am a complete Java NOOB. I want to handle multiple
I have a multi part question for a php script file. I am creating
Apologies for this question but I am a bit of a noob with Delphi.
I'm a complete Flex noob, so I apologize in advance if I'm missing something
I have a bunch of elements like the following: <div class=droppableP id=s-NSW style=width:78px; height:63px;

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.