Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails -- use type column without STI?

I want to use a column called type without invoking Single Table Inheritance (STI) - I just want type to be a normal column that holds a String.

How can I do this without having Rails expecting me to have single table inheritance and throwing an exception of The single-table inheritance mechanism failed to locate the subclass...This error is raised because the column 'type' is reserved for storing the class in case of inheritance.?

Any ideas on how to do this?

like image 601
Kvass Avatar asked Aug 20 '11 20:08

Kvass


2 Answers

In Rails 3.1 set_inheritance_column is deprecated, also you can just use nil as a name, like this:

class Pancakes < ActiveRecord::Base     self.inheritance_column = nil     #... end 
like image 109
Valentin Nemcev Avatar answered Oct 01 '22 09:10

Valentin Nemcev


You can override the STI column name using set_inheritance_column:

class Pancakes < ActiveRecord::Base     set_inheritance_column 'something_you_will_not_use'     #... end 

So pick some column name that you won't use for anything and feed that to set_inheritance_column.

like image 21
mu is too short Avatar answered Oct 01 '22 10:10

mu is too short