0

For my classes

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] I am getting the following error, when I try to call 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] when I try to call Customer.all the query runs like this,

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

I am not able to understand why it is '0' it should be '1'.

ayush lodhi
  • 367
  • 1
  • 4
  • 16

1 Answers1

1

The full STI class name is stored in the column user_type and not a '0' or a '1'. Check the find_sti_class class method.

Here the method is trying to find a class with the name 1

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

Change your scopes to

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

As for the Customer.all query. I do not know how it generates this query but maybe you have overridden something when you defined those scopes.

Oss
  • 4,232
  • 2
  • 20
  • 35