# Unidirectional belongs\_to polymorphic

**URL:** <https://www.ruby-forum.com/t/unidirectional-belongs-to-polymorphic/140230>\
**Category:** Rails\
**Created:** [June 12, 2008, 2:19am UTC](https://www.ruby-forum.com/t/unidirectional-belongs-to-polymorphic/140230 "2008-06-12T02:19:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [June 12, 2008, 2:19am UTC](https://www.ruby-forum.com/t/unidirectional-belongs-to-polymorphic/140230/1 "2008-06-12T02:19:09Z")

</div>

The setup:  
class Location::Base \< ActiveRecord::Base  
set\_table\_name :locations  
…  
end  
class Location::Address \< Location::Base  
…  
end  
class Location::Airport \< Location::Base  
…  
end  
class Person \< ActiveRecord::Base  
belongs\_to :location, :polymorphic =\> true, :class\_name =\>  
“Location::Base”  
…  
end

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.

-Gaius

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [June 12, 2008, 5:34am UTC](https://www.ruby-forum.com/t/unidirectional-belongs-to-polymorphic/140230/2 "2008-06-12T05:34:45Z")

</div>

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’  
end

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

-Gaius
