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 7577563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:13:13+00:00 2026-05-30T17:13:13+00:00

I have a difficult filtering problem in my Rails app. The user wants to

  • 0

I have a difficult filtering problem in my Rails app. The user wants to filter items using tags. This is no problem, I just use a named scope. But the tags come in boxes that the user can choose from, like this:

Size
  [XS]
  [S]

Color
  [Red]
  [Blue]

The tags are in “tagsets”, one tagset to many tags.

My question: how can I chain together multiple filter queries when the user wants to narrow down the search?

When the user picks XS from the first box, it should filter the result set on that tag alone. When the user picks XS and S, we add to the filter and get more results. Still not a problem with a named scope. But what if the user wants to filter XS and S and then Red color from the second box? That’s when things get hairy – I can’t just tack on another Item.by_tag(“tagname”) for that tag, it needs to be a tag filter on the results I already have, narrowing the results down.

Multiple checkboxes in the same tagset box – more results. A check in another tagset box – fewer results. I tried doing one SELECT for each tagset box and then merging the result sets in ruby to show only the hits that were in all the sets, but that breaks the pagination since I never know how many results will be returned. It feels like this needs to be one query and it’s probably some kind of join, but I’m not great at SQL and I can’t figure it out. This is what the relevant parts of the schema looks like:

create_table "items" do |t|
  t.string   "title"
end

create_table "items_tags", :id => false do |t|
  t.integer "item_id"
  t.integer "tag_id"
end

create_table "tags" do |t|
  t.string   "title"
  t.integer  "tagset_id"
end

create_table "tagsets" do |t|
  t.string   "title"
end
  • 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-30T17:13:14+00:00Added an answer on May 30, 2026 at 5:13 pm

    It seems that Rails doesn’t provide a way to build an OR query. So you have to do it by yourself.
    According to the schema, an Item has_and_belongs_to_many :tags, right?
    In Rails 2.3, following named_scopes should work. If you’re using Rails 3, you need to rewrite it.

    named_scope :having_tags, lambda { |tagsets| {:joins => :tags, :select => "DISTINCT items.*",
      :conditions => ([Array.new(tagsets.size, "tags.title IN (?)").join(' OR ')] + tagsets) }
    

    Then you call it like this:

    Item.having_tags([['XS', 'S'], ['Red', 'Blue']])
    

    I am assuming that you’re using MySQL.

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

Sidebar

Related Questions

I have run into a common, yet difficult problem. I do not use Photoshop
I have found it difficult to send exceptions of my Rails 3 app via
Hello everyone I have a difficult problem with using BULK INSERT command when I
I'm not sure how difficult this but I have an array and would like
I am currently faced with a difficult sorting problem. I have a collection of
I have a list of items that are in this format: 05/01 – Some
I have a difficult dilemma. I implemented a java application using Netbeans while implementing
I have a difficult layout in my website and I have a problem now
Hi I have a difficult regex problem that I have tried and have a
Analyzing the location.hash with this simple javascript code: <script type="text/javascript">alert(location.hash);</script> I have a difficult

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.