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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:04:33+00:00 2026-05-23T16:04:33+00:00

In ActiveRecord, has_many :through, and Polymorphic Associations , the OP’s example requests ignoring the

  • 0

In ActiveRecord, has_many :through, and Polymorphic Associations, the OP’s example requests ignoring the possible superclass of Alien and Person (SentientBeing). This is where my question lies.

class Widget < ActiveRecord::Base
  has_many :widget_groupings

  has_many :people, :through => :widget_groupings, :source => :person, :source_type => 'Person'
  has_many :aliens, :through => :widget_groupings, :source => :alien, :source_type => 'Alien'
end

SentientBeing < ActiveRecord::Base
  has_many :widget_groupings, :as => grouper
  has_many :widgets, :through => :widget_groupings
end


class Person < SentientBeing
end

class Alien < SentientBeing
end

In this modified example the grouper_type value for Alien and Person are now both stored by Rails as SentientBeing (Rails seeks out the base class for this grouper_type value).

What is the proper way to modify the has_many‘s in Widget to filter by type in such a case? I want to be able to do Widget.find(n).people and Widget.find(n).aliens, but currently both of these methods (.people and .aliens) return empty set [] because grouper_type is always SentientBeing.

  • 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-23T16:04:33+00:00Added an answer on May 23, 2026 at 4:04 pm

    Have you tried the simplest thing – adding :conditions to the has_many :throughs?

    In other words, something like this (in widget.rb):

    has_many :people, :through => :widget_groupings, :conditions => { :type => 'Person' }, :source => :grouper, :source_type => 'SentientBeing'
    has_many :aliens, :through => :widget_groupings, :conditions => { :type => 'Alien' }, :source => :grouper, :source_type => 'SentientBeing'
    

    JamesDS is correct that a join is needed – but it’s not written out here, since the has_many :through association is already doing it.

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

Sidebar

Related Questions

I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
Can't wrap my head around this... class User < ActiveRecord::Base has_many :fantasies, :through =>
I have this: class User < ActiveRecord::Base has_many :serials has_many :sites, :through => :series
I have the usual polymorphic associations for comments: class Book < ActiveRecord::Base has_many :comments,
I understand why ActiveRecord can't support has_many :through on polymorphic classes. But I would
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a basic has_many through relationship: class Foo < ActiveRecord::Base has_many :bars, :dependent
Given the following models: class Recipe < ActiveRecord::Base has_many :recipe_ingredients has_many :ingredients, :through =>
If I have something like this: class Post < ActiveRecord::Base has_many :comments, :as =>

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.