title: Using dual polymorphism through a join table in Rails 4
body: <p>I am trying to use polymorphism to help reduce duplication in my Rails 4 app, but am not having much luck. Conceptually, I have Computers and I have Printers, both of which I think could be considered Equipment. These items are assigned to either Employees or Facilities, both of which function as Owners. I also need to track chain of custody, so I thought simply adding some datetime fields to a join table would allow me to accomplish what everything I wanted. </p>
  
  <p>Code so far:</p>
  
  <pre><code>class Facility &lt; ActiveRecord::Base
    has_many   :computers, as: :equipment
    has_many   :printers,  as: :equipment
    belongs_to :owners,    polymorphic: true
  end
  
  class Employee &lt; ActiveRecord::Base
    has_many   :computers, as: :equipment
    has_many   :printers,  as: :equipment
    belongs_to :owners,    polymorphic: true
  end
  
  class Computer &lt; ActiveRecord::Base
    belongs_to :equipment,  polymorphic: true
    has_many   :facilities, as: :owners
    has_many   :employees,  as: :owners
  end
  
  class Printer &lt; ActiveRecord::Base
    belongs_to :equipment,  polymorphic: true
    has_many   :facilities, as: :owners
    has_many   :employees,  as: :owners
  end
  
  # Join table structure
  create_table "equipment_owners", force: true do |t|
    t.integer  "equipment_id"
    t.string   "equipment_type"
    t.integer  "owner_id"
    t.string  "owner_type"
    t.datetime "possession_datetime"
    t.datetime "relinquish_datetime"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  </code></pre>
  
  <p>Most examples of how to use polymorphic associations are singular (i.e. <a href="http://guides.rubyonrails.org/association_basics.html#polymorphic-associations" rel="nofollow">Rails guide</a> &amp; <a href="http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast" rel="nofollow">Railscast #154</a>), and so I tried to follow those structures as best I could. Unfortunately, testing the association in Rails console shows errors:</p>
  
  <pre><code>irb &gt; Computer.find_by_id(9).facilities
  Facility Load (0.2ms)  SELECT "facilities".* FROM "facilities" WHERE "facilities"."owners_id" = ? AND "facilities"."owners_type" = ?  [[nil, 9]]
  SQLite3::SQLException: no such column: facilities.owners_id: SELECT "facilities".* FROM "facilities"  WHERE "facilities"."owners_id" = ? AND "facilities"."owners_type" = ?
  </code></pre>
  
  <p>This suggests that I need to tell ActiveRecord to look in the EquipmentOwners table. Unfortunately, if I update my has_many to <code>has_many :facilities, as: :owners, through: :equipment_owners</code>, I get the following error:</p>
  
  <pre><code>irb &gt; Computer.find_by_id(9).facilities
  Computer Load (0.4ms)  SELECT "computers".* FROM "computers" WHERE "computers"."id" = 9
  ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :equipment_owners in model Computer
  </code></pre>
  
  <p>Can anyone suggest a way to make this work, or is this just beyond what I can do using standard Rails associations? Is the answer to make an <code>equipment_owner_id</code> in  all of my tables? That seems to sloppy, but it's my only idea currently.</p>