Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails double-entry method

1-account.rb

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-migration schema

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

Question1: What's the best method to reach supplier and expense in the view (see the picture below)?

Question2: How can I implement a method that automatically record the transaction amount in expense_debit and supplier_credit, and vice versa? (View screenshot)

like image 236
blawzoo Avatar asked Apr 14 '26 21:04

blawzoo


1 Answers

I'd suggest an alternative setup. For one thing, your transaction does not record which is the "from" account and which is the "to" account. That's kind of important to know for later.

I recognise that there may be multiple accounts from and to... but debit vs credit should be recorded on a per-transaction basis, not in just one big lump on the Account, otherwise it'll be difficult later to, say, list the change in an account's value between date A and date B (important for tax-returns or quarterly sales reports or whatever).

i don't know about naming, but perhaps a transaction has_many account-transfers and a transfer is "transaction_id, account_id, amount, direction" (where direction is debit/credit).

like image 118
Taryn East Avatar answered Apr 17 '26 10:04

Taryn East