# Polymorphic One-to-One

**URL:** <https://www.ruby-forum.com/t/polymorphic-one-to-one/129344>\
**Category:** Rails\
**Created:** [February 17, 2008, 5:45am UTC](https://www.ruby-forum.com/t/polymorphic-one-to-one/129344 "2008-02-17T05:45:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jarrod](https://www.ruby-forum.com/u/jarrod)\
**Post date:** [February 17, 2008, 5:45am UTC](https://www.ruby-forum.com/t/polymorphic-one-to-one/129344/1 "2008-02-17T05:45:41Z")

</div>

I’m having some trouble with a polymorphic relationship. Let’s say I  
have a class, Vehicle. Vehicle has some attributes of its own, but it  
largely delegates the specifics to a polymorphic association.

class Vehicle \< ActiveRecord::Base  
belongs\_to :mode, :polymorphic =\> true  
end

Simple enough. Then I have several other classes representing the  
actual vehicles. There’s a Car, a Boat, a Train, and so on. Here is  
the Car class:

class Car \< ActiveRecord::Base  
has\_one :vehicle, :as =\> :mode  
end

Now I ought to be able to load up my app and do something like:

v = Vehicle.find(1)  
puts v.mode.some\_attribute

Unfortunately, there’s a disconnect between Rails and my DB Schema.  
Specifically, the Cars, Boats, and Trains tables don’t have their  
own :id. Since they have a one-to-one relationship with Vehicle, the  
primary key on the Cars table is also the foreign key.

Here’s my database:

CREATE TABLE vehicles (  
“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
“top\_speed” integer DEFAULT NULL,  
“passenger\_capacity” integer DEFAULT NULL,  
“mode\_id” integer DEFAULT NULL,  
“mode\_type” varchar(255) DEFAULT ‘Car’  
);

CREATE TABLE cars (  
“vehicle\_id” integer DEFAULT NULL,  
“horsepower” integer DEFAULT NULL  
);

CREATE TABLE boats (  
“vehicle\_id” integer DEFAULT NULL,  
“captain” varchar(255) DEFAULT NULL  
);

When accessing vehicle.mode, I run into a SQLException:  
“no such column: cars.id: SELECT \* FROM cars WHERE(cars.id = 1)”

I can clearly see why I’m getting this error. The issue is that  
changing the DB Schema to add an ID column to Cars and Boats, etc…,  
however conventional it may be, is not necessarily practical in this  
situation.

How can I overcome this? Any thoughts would be welcome…

---

<div class="post-metadata">

**Author:** ![jarrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jarrod](https://www.ruby-forum.com/u/jarrod)\
**Post date:** [February 17, 2008, 5:47am UTC](https://www.ruby-forum.com/t/polymorphic-one-to-one/129344/2 "2008-02-17T05:47:26Z")

</div>

More specifically, the cars, boats, and trains tables don’t have a  
primary key at all. They do have a unique index on the ‘vehicle\_id’  
column, though, to keep things unique.

---

<div class="post-metadata">

**Author:** ![jarrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jarrod](https://www.ruby-forum.com/u/jarrod)\
**Post date:** [February 18, 2008, 1:40am UTC](https://www.ruby-forum.com/t/polymorphic-one-to-one/129344/3 "2008-02-18T01:40:40Z")

</div>

Unfortunately, your data model is backwards from what it needs to be  
to accomplish the polymorphic association. Whenever I teach  
associations I try to get people to think in terms of DNA – if you  
hold the primary key ‘DNA’ then you ‘belong\_to’ some other model and  
that model has\_one or \_many of you.

Thus, your Vehicle class needs to have a mode\_type and mode\_id  
column… and your modes (Car, etc) need to have an id. The  
‘mode\_type’ and ‘mode\_id’ will be used by Rails (with help of  
the :polymorphic parameter) to look up the appropriate ‘mode’. Rails  
can do some magic, but you’ve got to give it some help 😉
