SUMMARY: How do I assign objects, of different subclasses of an abstract
class, to a belongs_to relation?
I had the following class hierarchy:
Location < ActiveRecord::Base
Address < Location
Building < Location
and a class Person with these relationships:
has_one :address, :dependent => :destroy
belongs_to :building
Location is backed by table “locations,” and Person by “People.”
I had to refactor to insert an abstract class between Location and
Building:
Location < ActiveRecord::Base
Address < Location
SharedLocation < Location
Building < SharedLocation
DeletedLocation < SharedLocation
I now want Person#building to refer to either a Building or a
DeletedLocation. Refactoring the foreign key and accessor would be a
major pain.
I changed the relation for Person#building to:
belongs_to :building, :class_name => ‘SharedLocation’,
:foreign_key => ‘building_id’
(:foreign_key because I got a deprecation warning on specifying
:class_name without one.)
I also moved
has_many :people, :dependent => :nullify
from Building to SharedLocation. In the course of flailing about for a
solution, I added “:foreign_key => ‘building_id’”, but I don’t know what
I’m doing.
I ran my unit tests for setting the relation, and am now seeing
failures/errors, all of which seem to boil down to this: Attempting
person = Building.new(…)
gets an AssociationTypeMismatch, “SharedLocation expected, got
Building.”
How do I make belongs_to relations polymorphic?
Rails 2.3.2
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]