What is the _type column for polymorphism?

From the Rails Guide , in order to establish polymorphic relationships for one model, I need to add two columns for the corresponding table.

As shown in the figure below, the column is _id

used as a foreign key. But I can't figure out the use of the column _type

? What is its use?

enter image description here


source to share

1 answer

The column is _type

used to determine which resource it comes from. In this case, the polymorphic resource can be one of Employee

or Product

. In other words: the image can refer to either the product or the employee.

The column _type

will contain the line "Employee" or "Product". When this link is available, Rails will use it to find out which model to use to load the linked object.



All Articles