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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:40:58+00:00 2026-05-23T07:40:58+00:00

UPDATED/EDITED to clarify: I have the following defined in my Blog AR object scope

  • 0

UPDATED/EDITED to clarify:

I have the following defined in my Blog AR object

  scope :with_tag_tokens, lambda {|name_tokens| 
    joins(:tags).where(:tags => {:name_token => name_tokens}) unless name_tokens.empty?
  }

Whereas the following query would return all Blogs with Tags that have a “name_token” attribute equal to 1 OR 3 OR 5 (If I passed in [1,3,5] of course) … I want to know if there is a way to construct a similar query that would only return Blogs that are only associated to Tags with the “name_tokens” of 1 AND 3 AND 5.

In other words … I only want to return Blog A if it is associated to Tag 1 and Tag 3 and Tag 4.

Thanks -wg

  • 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-23T07:40:58+00:00Added an answer on May 23, 2026 at 7:40 am

    Whether or not this is an efficient way of handling this … it works

    tags = ['rails', 'ruby']
    BlogEntry.joins(:tags).where(:tags => {:name_token => tags}).group(BlogEntry.column_names.map {|c| BlogEntry.table_name + "." + c }.join(',')).having("count(#{BlogEntry.table_name + '.id'}) =#{tags.count}")
    

    What do ya’ll think? Is there a better way? Perhaps going straight-up sql is the proper approach here … if so, I’m definitely interested in hearing back from others good or bad.

    * UPDATE *
    Here is what the sql looks like in case folks are interested:

    SELECT \"blog_entries\".* 
    FROM \"blog_entries\" 
         INNER JOIN \"blog_entries_tags\" ON \"blog_entries_tags\".\"blog_entry_id\" = \"blog_entries\".\"id\" 
         INNER JOIN \"tags\" ON \"tags\".\"id\" = \"blog_entries_tags\".\"tag_id\" 
    WHERE \"tags\".\"name_token\" IN ('grace', 'gratitude') 
    GROUP BY id 
    HAVING count(blog_entries.id) =2 
    ORDER BY published_at DESC, title, created_at DESC
    

    Thanks

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

Sidebar

Related Questions

[Edited/Updated] Allow me to explain the situation. I have three files: Below is db.class.php.
Updated-2 I have interesting combination of warnings & errors. Firstly, when debugging, i get
UPDATED: I am setting default scope for some models in a runtime which seems
UPDATED: I have some table with sync_numbers, like this: --------------------- id | sync_number ---------------------
In my domain I have objects that are constantly being updated by a separate
I have a web page with an editable DetailsView. After it is (successfully) edited,
Edited:- Updated One What's wrong with this code. Now I am getting error on
Edited this question because of my bad examples.. Here is my updated question: Would
Is there a way to only load the new or updated (edited) records in
I would like to implement a JTable which will only be edited and updated

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.