Use column type without STI?

I want to use a column named "type" without calling Single Table Inheritance. I just want Type to be a normal column containing a row. How can I do this without having rails expecting me to have one table inheritance / return? 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?

+64
ruby-on-rails ruby-on-rails-3 single-table-inheritance
Aug 20 '11 at 20:41
source share
5 answers

In Rails 3.1, set_inheritance_column deprecated, you can also just use nil as a name, for example:

 class Pancakes < ActiveRecord::Base self.inheritance_column = nil #... end 
+112
Aug 08 2018-12-12T00:
source share

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, select a column name that you will not use for anything, and feed it to set_inheritance_column .

+19
Aug 20 '11 at 20:53
source share

I know this question is quite old and it deviates a little from the question you ask, but what I always do when it seems to me that the desire to name the column type or something_type is to search for a type synonym and use this instead:

Here are a few alternatives: view, sorting, variety, category, set, genre, view, order, etc.

+15
Feb 05 '16 at 20:14
source share

Rails 4.x

I ran into a problem in a Rails 4 application, but in Rails 4 the set_inheritance_column method does not exist at all, so you cannot use it.

The solution that worked for me was to disable unidirectional table inheritance by overriding the ActiveRecord s inheritance_column method, for example:

 class MyModel < ActiveRecord::Base private def self.inheritance_column nil end end 

Hope this helps!

+8
Apr 16 '15 at 1:42
source share

If you want to do this for all models, you can insert this into the initializer.

 ActiveSupport.on_load(:active_record) do class ::ActiveRecord::Base # disable STI to allow columns named "type" self.inheritance_column = :_type_disabled end end 
0
Oct 12 '16 at 19:56
source share



All Articles