Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Latest commit

 

History

History
18 lines (13 loc) · 781 Bytes

single_table_inheritance.md

File metadata and controls

18 lines (13 loc) · 781 Bytes

Single Table Inheritance

Summary: don't use Single Table Inheritance (STI), use separate tables instead.

Rails makes it possible to have multiple models stored in the same table and map these rows to the correct models using a type column. This can be used to for example store two different types of SSH keys in the same table.

While tempting to use one should avoid this at all costs for the same reasons as outlined in the document "Polymorphic Associations".

Solution

The solution is very simple: just use a separate table for every type you'd otherwise store in the same table. For example, instead of having a keys table with type set to either Key or DeployKey you'd have two separate tables: keys and deploy_keys.