2011-10-29 62 views
2

1- account.rbRails的複式方法

class Account < ActiveRecord::Base 
    belongs_to :transaction 
end 

class Supplier < Account 
end 

class Expense < Account 
end 

2- transaction.rb

class Transaction < ActiveRecord::Base 
    has_many :accounts 
    accepts_nested_attributes_for :accounts 
end 

3-遷移模式

create_table "accounts", :force => true do |t| 
    t.string "name" 
    t.decimal "debit" 
    t.decimal "credit" 
    t.decimal "balance" 
    t.string "type" 
    t.integer "transaction_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "transactions", :force => true do |t| 
    t.string "name" 
    t.decimal "amount" 
    t.date  "date" 
    t.string "document" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 
end 

問題1: 在視圖中達到供應商和費用的最佳方法是什麼(請參見下圖)?

問題2: 我如何能實現自動記錄expense_debit和交易金額,反之亦然的方法? (View screenshot

回答

1

我會建議一個替代設置。首先,您的交易不會記錄「來自」帳戶和「到」帳戶。以後這點很重要。

我認識到可能有多個帳戶來自...並且借記與貸記應記錄在每個交易的基礎上,而不僅僅是帳戶中的一個大塊,否則以後很難例如,列出日期A和日期B之間賬戶價值的變化(對於稅收或季度銷售報告或其他)非常重要。

我不知道命名,但可能是一個交易has_many賬戶轉移和轉移是「transaction_id,account_id,金額,方向」(其中方向是借方/貸方)。

+1

感謝您的回答Taryn East.I'm將嘗試您的解決方案。事實上,我從來沒有想過創建一個模型作爲交易和帳戶之間的鏈接。我希望帳戶傳輸模型將做的trick.I promess很快就會給你一個反饋。 – blawzoo