# Combining has\_many :through and polymorphic associations

**URL:** <https://www.ruby-forum.com/t/combining-has-many-through-and-polymorphic-associations/104844>\
**Category:** Rails\
**Created:** [May 23, 2007, 4:59am UTC](https://www.ruby-forum.com/t/combining-has-many-through-and-polymorphic-associations/104844 "2007-05-23T04:59:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 23, 2007, 4:59am UTC](https://www.ruby-forum.com/t/combining-has-many-through-and-polymorphic-associations/104844/1 "2007-05-23T04:59:59Z")

</div>

I’m trying to combine has\_many :through and polymorphic associations.

I’ve got it working one way but the other way it fails.

Here’s the domain I’m trying to model:

I’ve got a number of different authorable objects including  
Activities and Simulations. Each authorable object can be related to  
0 or more Subjects such as Math, Science, etc. I’d like to be able  
ask the following types of questions:

1. list all activities that have the subject Math
2. list all authorable objects hat have the subject Science
3. list all the subjects for an Activity
4. list all the subjects for a Simulation

I used this as a reference: [http://dev.rubyonrails.org/ticket/7143:](http://dev.rubyonrails.org/ticket/7143:)  
[PATCH] allow polymorphic :source for has\_many :through

I’ve tried this on edge Rails (rev 6786) and v1.2.3.

Here are my ActiveRecord models:

class Activity \< ActiveRecord::Base  
has\_many :subjectings  
has\_many :activity\_subjects, :through =\> :subjectings, :source =\>  
:subjectable, :source\_type =\> ‘Activity’  
end

class Simulation \< ActiveRecord::Base  
has\_many :subjectings  
has\_many :activity\_subjects, :through =\> :subjectings, :source =\>  
:subjectable, :source\_type =\> ‘Simulation’  
end

class Subject \< ActiveRecord::Base  
has\_many :subjectings  
has\_many :subject\_activities, :through =\> :subjectings, :source =\>  
:subjectable, :source\_type =\> ‘Activity’  
has\_many :subject\_simulations, :through =\> :subjectings, :source =\>  
:subjectable, :source\_type =\> ‘Simulation’  
end

class Subjecting \< ActiveRecord::Base  
belongs\_to :subject  
belongs\_to :subjectable, :polymorphic =\> true  
end

This works well from the Subject side of things but there is  
something wrong from the Activity or Simulation side of the  
association.

Here’s my schema:

create\_table “activities”, :force =\> true do |t|  
t.column “name”, :string  
end

create\_table “simulations”, :force =\> true do |t|  
t.column “name”, :string  
end

create\_table “subjectings”, :force =\> true do |t|  
t.column “subject\_id”, :integer  
t.column “subjectable\_id”, :integer  
t.column “subjectable\_type”, :string  
end

create\_table “subjects”, :force =\> true do |t|  
t.column “name”, :string  
end

Here’s the testing in script/console:

> > math.subject\_activities.push(activity\_fractions, activity\_decimals)  
> > math.subject\_simulations.push(simulation\_ratios, simulation\_base10)  
> > science.subject\_activities.push(activity\_density, activity\_batteries)  
> > science.subject\_simulations.push(simulation\_buoyancy, simulation\_circuits)

I can get the activities that have the subject “Math”:

> > math.subject\_activities.each { |a| puts “#{a.id}: #{a.name}” }; nil  
> > 1: Fractions  
> > 2: Decimals

I can also get the simulations that have the subject “Math”:

> > math.subject\_simulations.each { |s| puts “#{s.id}: #{s.name}” }; nil  
> > 1: Interactive Ratios  
> > 2: Powers of Ten

Or I can get all the authorable objects that have the subject “Math”:

> > math.subjectings.each { |s| puts “#{s.subjectable.id}:  
> > #{s.subjectable\_type}, name: #{s.subjectable.name}, class:  
> > #{s.subjectable.class}” }; nil  
> > 1: Activity, name: Fractions, class: Activity  
> > 2: Activity, name: Decimals, class: Activity  
> > 1: Simulation, name: Interactive Ratios, class: Simulation  
> > 2: Simulation, name: Powers of Ten, class: Simulation

But trying to get the subjects for an activity fails:

> > activity\_fractions.activity\_subjects  
> > ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column  
> > ‘subjectings.activity\_id’ in ‘where clause’: SELECT activities.\* FROM  
> > activities INNER JOIN subjectings ON activities.id =  
> > subjectings.subjectable\_id AND subjectings.subjectable\_type =  
> > ‘Activity’ WHERE ((subjectings.activity\_id = 1))

Thanks for any pointers.
