Invalid single table inheritance type .. not a subclass

I try to record every purchase that a user makes through many-to-many relationships between users and transactions, and link them through the transactuins_users connection table. But I have two problems: firstly, I must provide the creation! method with the object that he called on id, I thought that Rails had to figure it out on its own, given the association.

In addition, whenever I call the purchase method, I get the error message "Invalid inheritance type of one table: buy is not a subclass of transaction"

class User < ActiveRecord::Base

  # Associations
  has_and_belongs_to_many :transactions

  def purchase(package)
    return false unless funds_available?(package) and !owns?(package)
      package.with_lock do

      # Makes transaction
      package.user_id = id
      package.save!
      withdraw(package.cost)

      # Records transaction
      values = {user_id: id, type: "buy", cost: package.cost}
      transactions.create values
    end
  end


class Transaction < ActiveRecord::Base

  # Associations 
  has_and_belongs_to_many :transactions

  # Validations
  validates :user_id, :cost, presence: true
+4
source share
3 answers

, . - "", . , STI, - , "kind" "category".

, .

+13

self.inheritance_column = nil . . .

+9

. , , , - , type, , .

type . , type: "buy", buy , buy .

:

Parameters: {"step"=>{"type"=>"Email Template", "step_name"=>"Step name", "no_of_days"=>20, "cadence_id"=>nil, "email_template_id"=>nil}}

Completed 500 Internal Server Error in 238ms

ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: Email Template is not a subclass of Step):
   app/controllers/steps_controller.rb:15:in `create'

type, , self.inheritance_column = nil user . . , .

, -. :)

+4

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


All Articles