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
has_and_belongs_to_many :transactions
def purchase(package)
return false unless funds_available?(package) and !owns?(package)
package.with_lock do
package.user_id = id
package.save!
withdraw(package.cost)
values = {user_id: id, type: "buy", cost: package.cost}
transactions.create values
end
end
class Transaction < ActiveRecord::Base
has_and_belongs_to_many :transactions
validates :user_id, :cost, presence: true
manis source
share