ActiveRecord, has_many :through, and Polymorphic Associations?

You're so close. The problem is you're misusing the :source option. :source should points to the polymorphic belongs_to relationship.

Then all you need to do is specify :source_type for the relationship you're trying to define This fix to the Widget model should allow you do exactly what you're looking for class Widget :widget_groupings, :source => :grouper, :source_type => 'Person' has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien' end.

You're so close. The problem is you're misusing the :source option. :source should points to the polymorphic belongs_to relationship.

Then all you need to do is specify :source_type for the relationship you're trying to define. This fix to the Widget model should allow you do exactly what you're looking for. Class Widget :widget_groupings, :source => :grouper, :source_type => 'Person' has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien' end.

Oh my god that is so painfully obvious I cannot believe I glazed right over it. Thanks EmFi! – Cory Nov 6 '09 at 15:58 No problem, I think I agonized for about a day over how to do this the first time I encountered it.

Didn't help that it was one of the first things I tried to do in Rails that didn't involve following a tutorial/book. – EmFi Nov 6 '09 at 17:28 Thank you EmFi. – jdl Nov 11 '10 at 23:08 Doesn't seem to work anymore with ActiveRecord 3.1.1.

– Aymeric Oct 24 at 16:30.

Has many :through and polymorphic don't work together. If you try to access them directly, it should throw an error. If I am not mistaken, you have to hand write widget.

People and the push routine. I don't think it is a bug, it is just something which hasn't been implemented yet. I would imagine we see it in the feature, because everyone has a case in which they could use it.

1 They do work together. For example: has_many :subscriptions, :as => :subscribable has_many :subscribers, :through => :subscriptions, :source => :user – ScottJ Nov 6 '09 at 20:46 I will throw up an example of my failing code as a separate post in the near future :) It would save me alot of headache to figure out how to bypass that error. – cgr Nov 7 '09 at 1:35.

And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. However, when I do this, I've noticed the 'type' of the 'grouper' is always null in widget_groupings. Then all works as I would have normally expected.

I don't think I've ever seen this occur with non polymorphic associations and just wanted to know if this was something specific to this use case or if I'm potentially staring at a bug. Thanks for any help!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions