简体   繁体   中英

Ruby on Rails - proper way to refactor this (single table inheritance?) model

I have a model which can be one of many types. Let's say the model is a Vehicle. The vehicle can be a car, plane, train or bicycle. (sorry for lack of a better example).

Ideally I will later have a CRUD form where the type can be created. Is it better to take my existing vehicle class and make it a single table inherited model with a type field? should I create a VehicleType model and have a 1:1 mapping?

Most importantly, which way would make creating the form easiest? The user will be created a Vehicle on the form and then there will be a radio drop down where the vehicle type should be selected. A quick example would be great!

Yes, Single Table Inheritance would be a good solution to this, as long as the subtypes are similar enough that you'd like to store them all in the same table. Alex Reisner has a good article to help you decide whether it's helpful for your situation.

If you read through this thread , you'll find a couple approaches to building a creation form with a "type" drop down or radio button gruop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM