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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:02:26+00:00 2026-05-30T16:02:26+00:00

I need a little help with Rails. I am transferring a backend system of

  • 0

I need a little help with Rails. I am transferring a backend system of data look up services from PHP to RoR. Which I am finding to be surprisingly concise.

I need to be able to search a model on its relation with a tag model. Each model has been given the has_and_belongs_to_many association.

When the search is passed in with a comma separated list of values I need to perform an ‘AND’ search on the tags so I can find all resources that have all tags passed (i.e. London AND bridges).

Also it needs to be case insensitive. Currently I have only a single tag search being performed in the Resource model.

def self.search(search)
    where('LOWER(tags.name) = ?', search.downcase).joins(:tags)
end

All my attempts at using an array using either where, find, or all have not been successful.

The PHP way of doing an AND search on a join table seemed rather hacky, I am hoping Rails can provide a more elegant solution.

Any help would be greatly appreciated.

  • 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-30T16:02:28+00:00Added an answer on May 30, 2026 at 4:02 pm

    If you need an AND, then you need to ensure that the number of matching tags is precisely the number you requested, so that involves a COUNT as well. The query you’re gunning for will involve IN.

    It would be more convenient if you could ensure that your tags are always stored in lower case. Doing operations like LOWER(...) in a query is going to be trouble if you have a lot of data since it results in an automatic table scan and cannot be indexed.

    Secondly, I’d avoid using has_and_belongs_to_many as this is a relic from the Rails 1.0 days that isn’t as flexible or reliable as the has_many :through alternative. The only difference between the two approaches from a database perspective is that the :through join table has a unique ID for each entry and is represented by a first-class model. The has_and_belongs_to_many system is quirky to the point of uselessness a lot of the time.

    What you’re probably wanting is something that looks like this:

    def self.with_all_tags(*tags)
      tags = tags.flatten.collect(&:downcase)
      where('tags.name IN (?)', tags).having('COUNT(tags.id)=?', tags.length).joins(:tags)
    end
    
    def self.with_any_tags(*tags)
      tags = tags.flatten.collect(&:downcase)
      where('tags.name IN (?)', tags).joins(:tags)
    end
    

    You may need to experiment to get the right behaviour. Your example wasn’t that specific.

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

Sidebar

Related Questions

I need a little help getting a tar file to download from a website.
I need a little help here: I get a file from an HTML upload
I need a little help with updating my UI from Runnable/Handler every second. I'm
I need a little help about a PHP FB app. Matter is that I
I need a little help with the fsockopen function. I have this PHP code:
I need a little help with php_auth I have used HTTP authentication with Php
I'm just figuring out my way around rails but I need a little help
Need a little help from you guy's. I am making a check where element
I need a little help here with a query. I want to count from
Need a little help with a SQL / ActiveRecord query. Let's say I have

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.