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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:59:02+00:00 2026-05-24T12:59:02+00:00

Is this doable? I have the following scope: class Thing < ActiveRecord::Base scope :with_tag,

  • 0

Is this doable?

I have the following scope:

class Thing < ActiveRecord::Base

scope :with_tag, lambda{ |tag| joins(:tags).where('tags.name = ?', tag.name)
                                           .group('things.id') }

def withtag_search(tags)
  tags.inject(scoped) do |tagged_things, tag|
    tagged_things.with_tag(tag) 
  end
end

I get a result if there’s a single tag in the array of tags passed in with Thing.withtag_search(array_of_tags) but if I pass multiple tags in that array I get an empty relation as the result. In case it helps:

Thing.withtag_search(["test_tag_1", "test_tag_2"])

SELECT "things".* 
FROM "things" 
INNER JOIN "things_tags" ON "things_tags"."thing_id" = "things"."id" 
INNER JOIN "tags" ON "tags"."id" = "things_tags"."tag_id" 
WHERE (tags.name = 'test_tag_1') AND (tags.name = 'test_tag_2') 
GROUP BY things.id

=> [] # class is ActiveRecord::Relation

whereas

Thing.withtag_search(["test_tag_1"])

SELECT "things".* 
FROM "things" 
INNER JOIN "things_tags" ON "things_tags"."thing_id" = "things"."id" 
INNER JOIN "tags" ON "tags"."id" = "things_tags"."tag_id" 
WHERE (tags.name = 'test_tag_1') 
GROUP BY things.id

=> [<Thing id:1, ... >, <Thing id:2, ... >] # Relation including correctly all 
                                            # Things with that tag

I want to be able to chain these relations together so that (among other reasons) I can use the Kaminari gem for pagination which only works on relations not arrays – so I need a scope to be returned.

  • 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-24T12:59:03+00:00Added an answer on May 24, 2026 at 12:59 pm

    I also ran into this problem. The problem is not Rails, the problems is definitely MySQL:

    Your SQL will create following temporary JOIN-table (only neccesary fields are shown):

    +-----------+-------------+---------+------------+
    | things.id | things.name | tags.id | tags.name  |
    +-----------+-------------+---------+------------+
    |     1     |     ...     |    1    | test_tag_1 |
    +-----------+-------------+---------+------------+
    |    1      |     ...     |    2    | test_tag_2 |
    +-----------+-------------+---------+------------+
    

    So instead joining all Tags to one specific Thing, it generates one row for each Tag–Thing combination (If you don’t believe, just run COUNT(*) on this SQL statement).
    The problem is that you query criteria looks like this: WHERE (tags.name = 'test_tag_1') AND (tags.name = 'test_tag_2') which will be checked against each of this rows, and never will be true. It’s not possible for tags.name to equal both test_tag_1 and test_tag_2 at the same time!

    The standard SQL solution is to use the SQL statement INTERSECT… but unfortunately not with MySQL.

    The best solution is to run Thing.withtag_search for each of your tags, collect the returning objects, and select only objects which are included in each of the results, like so:

    %w[test_tag_1 test_tag_2].collect do |tag|
      Thing.withtag_search(tag)
    end.inject(&:&)
    

    If you want to get this as an ActiveRecord relation you can probably do this like so:

    ids = %w[test_tag_1 test_tag_2].collect do |tag|
      Thing.withtag_search(tag).collect(&:id)
    end.inject(&:&)
    Things.where(:id => ids)
    

    The other solution (which I’m using) is to cache the tags in the Thing table, and do MySQL boolean search on it. I will give you more details on this solution if you want.

    Anyways I hope this will help you. 🙂

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

Sidebar

Related Questions

I have following class. In this, Iris is another class with some attributes. public
I have this following piece of code: public TimestampedRowStorage GetTimestampedRowStorage(string startTime, string endTime, long
We have a VB.net function with the following signature in class InitializerFactory: Public Shared
Basically I have a Base class called Program. I then have more specific program
I have the following c++ classes: Page.h: #ifndef PAGE_H_ #define PAGE_H_ #include Process.h class
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
I have a 2884765579 bytes file. This is double checked with this function, that
If I have a double (234.004223), etc., I would like to round this to
Microsoft J++ does not have a Double.parseDouble() method. How do I do this?

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.