One-time table inheritance does not work in Rails 4 application - ActiveRecord :: SubclassNotFound: single table inheritance mechanism failed

I have a new Rails 4 application in which I created several models in the STI configuration.

The main model is called Orderand inherited from ActiveRecord::Base. Here's what it looks like:

class Order < ActiveRecord::Base
  TYPES = %w(remote local)

  scope :remotes,  -> { where(type: 'remote') }
  scope :locals,   -> { where(type: 'local') }
end

The other two models are in the folder in models/orders, and they are called Remoteand Local, and they both inherit fromOrder

The order migration file is as follows:

def change
  create_table :orders do |t|
    t.string :source_url, null: false
    t.string :final_url, null: false

    t.string :type

    t.string :category

    t.timestamps
  end
end

I also made sure that I included dir models/ordersin Rails by doing:

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]

, Order.all, , . Remote Order.all :

>> Order.all
Order Load (1.0ms)  SELECT "orders".* FROM "orders"
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate
the subclass: 'Remote'. This error is raised because the column 'type' is reserved
for storing the class in case of inheritance. Please rename this column if you didn't
intend it to be used for storing the inheritance class or overwrite
Order.inheritance_column to use another column for that information.

? ?

.

+4
3

, , ...

, , "". , , , , "type" , , , .

-, , , ActiveRecord , , .

, - Rails , .

+3

, , , ?

+2

"" ActiveRecord. , :

self.inheritance_column = :_type_disabled
+2

Source: https://habr.com/ru/post/1526010/


All Articles