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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:26:52+00:00 2026-06-09T18:26:52+00:00

I have a simple tagging system of items with the following structure: class Item

  • 0

I have a simple tagging system of items with the following structure:

 class Item < ActiveRecord::Base
     has_many :taggings, :dependent => :destroy
     has_many :tags, :through => :taggings
 end

 class Tagging < ActiveRecord::Base
     belongs_to :tag
     belongs_to :item
 end

 class Tag < ActiveRecord::Base
     has_many :taggings, :dependent => :destroy
 end

I’d like to add a scope to the Item class to be able to get all the items with a given set of tags (all tags in the set included)

So I tried the following scope:

 scope :tag_context, lambda { |context| (context.empty? ? all :
       joins(:taggings).where(:taggings => {tag_id => context.collect(&:id)})
       )
  }

where context is an Array of Tag objects.

The point is that this scope yield the following sql:

SELECT items.* FROM items INNER JOIN taggings ON taggings.item_id = items.id 
                          WHERE taggings.tag_id IN (1,2)

assuming context contains tag 1 and 2.

I would like to get items that are tagged by tag 1 AND tag 2.
So I assume, something like:

SELECT items.* FROM items INNER JOIN taggings as t1 ON t1.item_id = items.id 
                          INNER JOIN taggings as t2 ON t2.item_id = items.id 
                          WHERE t1.tag_id = 1 AND t2.tag_id = 2

How should I proceed to translate it in a Rails scope?
I need a scope in order to be able to chain the various scopes on the Item class.

Thanks for your help!

  • 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-06-09T18:26:54+00:00Added an answer on June 9, 2026 at 6:26 pm

    You can try building the scope dynamically like this (and you don’t need an .empty? check with inject):

     scope :tag_context, lambda { |context| 
          context.collect(&:id).inject(self) do |m, id| 
            m.joins("INNER JOIN taggings as t#{id} ON t#{id}.item_id = items.id").
            where("t#{id}.tag_id = ?", id)
          end
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following 3 tables as part of a simple item tagging schema:
I have simple class with width and height member fields which define number of
I want to build a simple tagging system for my news section DB setup
I have a fairly simple model that uses Django Taggit for tagging. Everything works
Have simple Order - Item app. No cart. Need help with joined scopes for
I have simple form set up like the following: <form> <input type=text name=first_number id=first_number
I have simple divs <div class=ui-bar-a ui-corner-top> first_test | Status: <img src=templates/style/images/reload.gif /> </div>
I have simple static lib in xcode with the only class test.h: @interface TestClass
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (
I have simple php validation form that is halfway working. If you leave the

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.