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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:35:36+00:00 2026-06-15T10:35:36+00:00

I am using a polymorphic association of two different models to a tagging model.

  • 0

I am using a polymorphic association of two different models to a tagging model. Great, easy. But, one of these two models also belongs to the other model.

class Facility < ActiveRecord::Base
  has_many :units
  has_many :taggings, :as => :taggable
  has_many :tags, :through => :taggings
end
class Unit < ActiveRecord::Base
  belongs_to :facility
  has_many :taggings, :as => :taggable
  has_many :tags, :through => :taggings
end
class Tagging < ActiveRecord::Base
  belongs_to :taggable, :polymorphic => true
  belongs_to :tag
end
class Tag < ActiveRecord::Base
  has_many :taggings
end

I am trying to write a scope to retrieve all of the Units for a certain tag, including those Units belonging to a Facility with that tag. This does not work:

named_scope :by_tag_id, lambda {|tag_id| 
{ 
  :select => "DISTINCT units.*",
  :joins => [:taggings, {:facility => :taggings}],
  :conditions => ["taggings.tag_id = ? OR taggings_facilities.tag_id = ?", 
    tag_id.to_i, tag_id.to_i] 
}}

For that named_scope, I only get those units that have the tag.

Any thoughts? Am I missing something obvious here?

Update

I am, unfortunately, on Rails 2.3.x, Ruby 1.8.7.

I am trying right now some techniques with more explicitly written joins.

Update 2

I was not able to answer my own question because I have no reputation. Oops. I should really contribute more…

Well, I guess I just needed to poke and push a bit more. Here is what I have working:

named_scope :by_tag_id, lambda {|tag_id| 
  { 
    :select => "DISTINCT units.*",
    :joins => [
        "INNER JOIN facilities as bti_facilities ON units.facility_id = bti_facilities.id",
        "LEFT JOIN taggings AS bti_facilities_taggings 
          ON bti_facilities_taggings.taggable_id = bti_facilities.id AND bti_facilities_taggings.taggable_type = 'Facility'",
        "LEFT JOIN taggings AS bti_units_taggings ON bti_units_taggings.taggable_id = units.id AND bti_units_taggings.taggable_type = 'Unit'",
      ],
    :conditions => ["bti_units_taggings.tag_id = ? OR bti_facilities_taggings.tag_id = ?", tag_id.to_i, tag_id.to_i] 
  }
}

I am using some intentionally obfuscated table aliases in order to try to avoid collision with other named scopes. (I did not have facilities aliased to start and I ran into a SQL error on the query with I tried to chain with another scope that referenced the facilities table.)

If anyone has a better answer, or some feedback on my method, I would love to hear it.

  • 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-15T10:35:38+00:00Added an answer on June 15, 2026 at 10:35 am

    I see that you’ve already answered your question, but if you were to use :include => instead of :join => in the original query, it would generate a LEFT OUTER JOIN. That said, you may not need to eager load the association, only query on it, so your mileage may vary.

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

Sidebar

Related Questions

I am using attachment model as polymorphic association. How to change path and url
I am using polymorphic association. I have 2 models articles and events which have
I am using Polymorphic Models . Simple Question: My code below works without using
I'm using nested_form for a situation where: Parent (climb) ==has_one==> Join Model (route_ascent) ==polymorphic
I have two models, Article and Post that both inherit from a base model
I can create a record with the correct association using the create method, but
I'm using a polymorphic association in SQLAlchemy as described in this example . It
I'm using acts_as_commentable to in an application through a polymorphic association and wanted to
I am using polymorphic relation as i have 3 models like this: class Food
I have a polymorphic association ( contact_details ) in my Company model and I

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.