Is there a recommended approach to replace the reserved type column in Rails?

Viewed 42

I have a model that needs a type column, but it's not for single table inheritance. Is there a recommended way or workaround for this?

1 Answers

Yes. Just change the inheritance_column:

Defines the name of the table column which will store the class name on single-table inheritance situations.

The default inheritance column name is type, which means it's a reserved word inside Active Record. To be able to use single-table inheritance with another column name, or to use the column type in your own model for something else, you can set inheritance_column:

self.inheritance_column = 'zoink'

If you're not using STI you can set it to whatever you want. The end result is exactly the same as as with models that don't have a type column.

Related