最好的方式来模拟现金,信用卡交易在RoR



我想有一个名为"transactions"的表,但有不同的类(CashTransaction和CreditCardTransaction),并有一个"type"列决定哪一行是什么。

有什么建议吗?

如果两个事务类型具有相同的属性(表列),则可以使用STI,创建三个模型,如

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 ..

最新更新