ActiveRecord::SubclassNotFound,无论我选择什么列名

ActiveRecord::SubclassNotFound, no matter what column name I choose

为了我的类

class User < ActiveRecord::Base

  self.inheritance_column = :user_type
  scope :customers, -> { where(user_type: '1') }
  scope :freight_forwarders, -> { where(user_type: '2') }
end

class FreightForwarder < User

  has_many :quotes, foreign_key: "ff_id"

end

class Customer < User

  has_many :quotes, foreign_key: "ff_id"

end

[1] 当我尝试调用 User.customers

时出现以下错误

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: '1'. This error is raised because the column 'user_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 User.inheritance_column to use another column for that information.

[2] 当我尝试调用 Customer.all 时,查询是这样运行的,

SELECT "users".* FROM "users" WHERE "users"."user_type" IN (0)

我不明白为什么它是“0”它应该是“1”。

完整的 STI class 名称存储在列 user_type 中,而不是 '0''1'。检查 find_sti_class class method.

这里的方法试图找到名称为 1

的 class

The single-table inheritance mechanism failed to locate the subclass: '1'.

将范围更改为

  scope :customers, -> { where(user_type: 'Customer') }
  scope :freight_forwarders, -> { where(user_type: 'FreightForwarder') }

至于Customer.all查询。我不知道它是如何生成这个查询的,但也许你在定义这些范围时覆盖了一些东西。