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…