0

「トランザクション」と呼ばれる1つのテーブルが必要ですが、どの行が何であるかを決定する「タイプ」列を持つ異なるクラス(CashTransactionとCreditCardTransaction)があります。

これを行う方法に関するアドバイスはありますか?

4

1 に答える 1

2

両方のトランザクション タイプが同じ属性 (テーブル列) を持っている場合、STI を使用できます。次のような 3 つのモデルを作成します。

class Transaction < ActiveRecord
    # create table called transactions and add type column to it.  
    # add common methods inside this class
end

class CashTransaction < Transaction
     # the type column will be CashTransaction and used to determine entry for this class in transactions table 
end

class CreditCardTransaction < Transaction    
    # the type column will be CreditCardTransaction and used to determine entry for this class in  transactions table 
end

そして、子クラスを使用してこれらのトランザクションを作成できます

CreditCardTransaction.new 
CreditCardTransaction.find 
 #..etc
 # or 
CashTransaction.new
CashTransaction.find ..
于 2013-04-11T18:57:18.270 に答える