Polymorphic One-to-One

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...

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.

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 :wink: