Unidirectional belongs_to polymorphic

The setup:
class Location::Base < ActiveRecord::Base
set_table_name :locations

class Location::Address < Location::Base

class Location::Airport < Location::Base

class Person < ActiveRecord::Base
belongs_to :location, :polymorphic => true, :class_name =>


I want a unidirectional belongs_to relationship. That is, Locations
are never going to need to figure out who’s at them. Or, more
specifically, the headache involved with the many-to-many and the
caching and everything isn’t worth the cost. I just want to be able
to store a location for a Person (and for various other models that
are unrelated to Person).

Some questions:

Why does Person need both a location_id and a location_type? In order
to instantiate the location, it needs to look up the whole row in the
locations table, which includes a “type” column (as required by single
table inheritance).

Why won’t the new sexy fixtures work on this (regardless of whether
Person has a location_type column)? They seem to work only if
Location::Base has_one :person.


Right, so the answer is that uni-directional belongs_to associations
can’t be polymorphic. The polymorphicness of a belongs_to
association is on the belonging-to side, not the belonged-side. That
is, my Person doesn’t inherit from some “Locatable.”

This solves it:
class Person < ActiveRecord::Base
belongs_to :location, :class_name => ‘Location::Base’

(I actually do abstract Locatable into a mixin module, but it’s not an
STI inheritance thing, so there’s no AR polymorphism.)