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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:56:49+00:00 2026-06-02T06:56:49+00:00

Im having a bit of an issue setting up a Rails has_many :through :polymorphic

  • 0

Im having a bit of an issue setting up a Rails has_many :through :polymorphic relationship.

I’m aware this subject is well documented on SO but i think my problem is down to my model and foreign_key names as opposed to syntax i.e i think this is a “i’ve been looking at code too long” issue that just requires another set of eyes.

Anyway i have the following setup:

class Milestone < ActiveRecord::Base
  has_many :responsible_items, :as => :responsibility
  has_many :responsible, :through => :responsible_items
end

class ResponsibleItem < ActiveRecord::Base
  belongs_to :responsible, :class_name => "User"
  belongs_to :responsibility, :polymorphic => true
end

class User < ActiveRecord::Base
  has_many :responsible_items, :foreign_key => :responsible_id
  has_many :responsibilities, :through => :responsible_items
end

This seems to work fine, without error, from the Milestone side of things. For example in terminal i can write:

Milestone.first.responsible

…and get an empty collection as i’d expect.

However, from the User side of things, running:

User.first.responsibilities

…is returning an AR error:

ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'User#responsibilities' on the polymorphic object 'Responsibility#responsibility'.

I’m assuming the issue is something to do with the fact that i am referring to the User relationship as :responsible. Is this right?

Any help would be much appreciated, thanks.

  • 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-02T06:56:50+00:00Added an answer on June 2, 2026 at 6:56 am

    Thanks to @Abid’s comment i got thinking about the logistics of pulling in all responsibilities for a user, which wasn’t going to be feasible. I needed to be more specific about what i wanted from the relationship and as result defining the following worked:

    class User < ActiveRecord::Base
      has_many :responsible_items, :foreign_key => :responsible_id
      has_many :milestone_responsibilities, :through => :responsible_items, :source => :responsibility, :source_type => 'Milestone'
    end
    

    I can then extend this as i add further polymorphic relationships on other models for example:

    class User < ActiveRecord::Base
      has_many :responsible_items, :foreign_key => :responsible_id
      has_many :milestone_responsibilities, :through => :responsible_items, :source => :responsibility, :source_type => 'Milestone'
      has_many :task_responsibilities, :through => :responsible_items, :source => :responsibility, :source_type => 'Task'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a strange issue here, i have this bit of code: <?php if
my issue is a bit similar to this one: Having an issue with my
I'm having a bit of an issue setting up our test site. In IIS
I'm having this issue with several screens in my app, but I'll explain what
I'm having a bit of an issue. For some reason, this class I'm trying
I'm having a bit of an issue with a query and scaling that query
hello fellow java developers. I'm having a bit of an issue here. I have
Having a bit of trouble wrapping my head around this one. I'm sure the
I'm having a bit of a weird issue here, basically, im using hibernate together
Having a bit of an issue with wordpress URLs, it seems to automatically add

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.