0

我想要一个名为“事务”的表,但具有不同的类(CashTransaction 和 CreditCardTransaction),其中“类型”列确定哪一行是什么。

关于如何做到这一点的任何建议?

4

1 回答 1

2

如果两种事务类型都具有相同的属性(表列),则可以使用 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 ..
于 2013-04-11T18:57:18.270 回答