April 2019
Beginner to intermediate
244 pages
6h 34m
English
Some frameworks, notably ActiveRecord and Laravel, have a built-in convention to support polymorphic relationships. Here’s how we might follow that convention with our notes feature.
Say that our notes table started with just these two columns:
| Field | Type |
|---|---|
|
notes |
text |
|
author |
string |
To support polymorphism, we would add two more columns that would indicate the table name and ID of the record we want to attach the note to. When naming these columns, the convention is to use an adjective that’s descriptive of the relationship, then add _type for the column that holds the table name, and _id for the ID. In our case, it might look like this:
| Field | Type |
|---|---|
|
notes |
text |
|
author |
string |
|
notable_type ... |
Read now
Unlock full access