Basic help with understanding RoR Inheritance

T

TomRossi7

I'm sorry for the newbie question, but I can't seem to find a basic
enough explanation for what I am looking for. Basically, I am trying
to understand Ruby on Rails Single Table Inheritance and the required
database structure for easy ActiveRecord integration. Here is a simple
example that I would be very grateful for assistance:

Imagine you want to create an application built for tracking vehicle
information. You have a super-class Vehicle with attributes like VIN,
manufacturer, manufacturer date etc. -- all of the attributes that are
shared by all vehicles. Then you have sub-classes like car, truck,
motorcycle, etc. These sub-classes have unique attributes like maybe a
"towing capacity" for trucks [Granted, the attributes aren't that
dissimilar between a car and a truck, but this is just an example].

So, how would this best be modeled in the database and RoR? I am
thinking a model (class) for Vehicle, Car, Truck, Motorcyle. The
tables would be vehicles, cars, trucks, motorcylces. They each would
have id's and the vehicle table would track for each id the "type" of
vehicle.

Am I on the right track?

Thanks,
Tom
 
J

James Edward Gray II

I'm sorry for the newbie question, but I can't seem to find a basic
enough explanation for what I am looking for. Basically, I am trying
to understand Ruby on Rails Single Table Inheritance and the required
database structure for easy ActiveRecord integration. Here is a
simple
example that I would be very grateful for assistance:

Imagine you want to create an application built for tracking vehicle
information. You have a super-class Vehicle with attributes like VIN,
manufacturer, manufacturer date etc. -- all of the attributes that are
shared by all vehicles. Then you have sub-classes like car, truck,
motorcycle, etc. These sub-classes have unique attributes like
maybe a
"towing capacity" for trucks [Granted, the attributes aren't that
dissimilar between a car and a truck, but this is just an example].

So, how would this best be modeled in the database and RoR? I am
thinking a model (class) for Vehicle, Car, Truck, Motorcyle. The
tables would be vehicles, cars, trucks, motorcylces. They each would
have id's and the vehicle table would track for each id the "type" of
vehicle.

Am I on the right track?

Close. Remember it's called "Single Table" Inheritance. That means
you only make a "vehicles" table, and add a "type" field.
ActiveRecord pretty much handles the rest.

There's information on this in the Rails book, in case you want to
know more.

James Edward Gray II
 
T

TomRossi7

James,

Thanks for the reply. I've ordered "Agile Web Development with Rails:
A Pragmatic Guide", but couldn't wait to get started in the framework.

For the example I gave, where would you store the unique attributes
associated with my subclasses? For example where would the
"TowingCapacity" field be located?

In my application each of the 10 subclass has around 20 unique
attributes. I hope that the solution won't require me to create one
table with over 200 fields (with each row having 180 NULL fields for
the non-applicable attributes).

Thanks!
 
J

James Edward Gray II

James,

Thanks for the reply. I've ordered "Agile Web Development with Rails:
A Pragmatic Guide", but couldn't wait to get started in the framework.

For the example I gave, where would you store the unique attributes
associated with my subclasses? For example where would the
"TowingCapacity" field be located?

In my application each of the 10 subclass has around 20 unique
attributes. I hope that the solution won't require me to create one
table with over 200 fields (with each row having 180 NULL fields for
the non-applicable attributes).

That's exactly the case, with Rails Single Table Inheritance. It's
hard for me if that's the best move, with this dataset since I don't
know too much about it.

Also, the Rails Mailing list might have better ideas about approach.

James Edward Gray II
 
L

Lou Vanek

You might try one 'Vehicle' table that holds all common
fields, and a bunch of other tables (Cars, Trucks, Motorcycles, etc.)
common to each vehicle that only holds fields specific to that type of vehicle.


class Trucks < ActiveRecord::Base
has_one :vehicle
. . .
end

class Cars < ActiveRecord::Base
has_one :vehicle
. . .
end

class Motorcycles < ActiveRecord::Base
has_one :vehicle
. . .
end

class Vehicles < ActiveRecord::Base
#belongs_to :truck # this probably wouldn't work. Not sure what to do here.
#fields common to all: trucks, cars, motorcycles, etc.
. . .
end

I haven't tried this type of schema but it seems like it might work.

-lv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top