Reputation: 6371
I have a model in Rails that I would like to use as a basis for another.
For example model: parent will be the template for model child. I can see two possible options:
(1) Inherit from the first model and then add additional columns
Class Parent < ActiveRecord::Base
Class Child < Parent
(2) Copy the model.rb file and add new features
Class Child < ActiveRecord::Base
In both cases the "Rails" part of the model is created, but what about the database table? I could create the table using create table child as select * from parent where 1=2 and then create migrations to add the additional columns, but it doesn't seem like the "Rails way".
Is there an easy way to create a migration based on an existing table. or am I barking up the wrong tree entirely?
Upvotes: 0
Views: 63
Reputation: 43298
Your (1) is called single table inheritance (STI). Basically you use one table that has both the fields of the parent and the child. You'll also need a column called type
to identify the type of the object.
Without more details I can't say if it's a good idea to use STI in your case, but (2) copying model.rb certainly doesn't seem right.
Upvotes: 2