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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:02:49+00:00 2026-05-27T23:02:49+00:00

I’m trying to do a simple Post/Tags relation in rails 3. Everything working fine

  • 0

I’m trying to do a simple Post/Tags relation in rails 3.
Everything working fine except when I want to query the Posts which are related to several tags.
Basically I’d like to do this kind of thing :

Post.joins(:tags).where('tags.name ILIKE ?', var)

But instead of having just one var, I’d like to use an array.
I tried :

Post.joins(:tags).where('tags.name IN (?)', names_array)

But unfortunately it does a simple LIKE (not ILIKE) and works like a OR condition which sounds perfectly logical.

I also found another solution by using find_by_sql in this post
http://snippets.dzone.com/posts/show/32

But it seems a bit ugly to me.

To better understand the problem.
I’ve got 3 posts :
PostA
PostB
PostC

PostA is related to TagA TagB and TagC tags.
PostB is related to TagA and TagB tags.
PostC is only related to TagA.

If I look for TagA and TagC Posts I’d like to finds PostA because it is related to both Tags.
Using a hash condition returns PostA PostB and PostC.
What I want is the Posts which are related to “at least” all the specified Tags.

So anyone has a better way to handle this ?

Thanks.

  • 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-27T23:02:49+00:00Added an answer on May 27, 2026 at 11:02 pm

    I don’t know if it will solve you problem but for complex queries like that I almost always just use Squeel.

    Then do something like this:

    @posts = Post.joins(:tags)
      .where{tags.name.like_any names_array}
      .group("post_id")
      .having("count(post_id) = #{names_array.size}")
    

    The SQL hopefully looks something like this

    SELECT "posts".* FROM "posts"
      INNER JOIN "tags" ON "tags"."post_id" = "posts"."id"
      WHERE (("tags"."name" LIKE "TagA" OR "tags"."name" LIKE "TagB"))
      GROUP BY post_id
      HAVING count(post_id) = 2
    

    If I remember squeel is pretty good at using ILIKE instead of LIKE depending on the database used. (atleast better than AR)

    Also you could do this using AR without squeel but I REALLY like some of the ideas and helpers that come with squeel like _all


    As for an explination…

    Assume I searched for TagsA and B.

    What that does is finds all the Posts with those tags.

    So you’ll have something like:

    • PostA TagA
    • PostA TagB
    • PostB TagA
    • PostB TagB
    • PostC TagA

    Then it will group all those different Post results by the joined tags using post_id.

    • PostA TagA TagB
    • PostB TagA TagB
    • PostC TagA

    Then it will check the number of Tags the SQL line has by checking how many forgien_ids are present.
    Since A and B have 2 tags you know it matched all you input.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.