Trasanction for polymorphic association

48 views
Skip to first unread message

Von Christian Halip

unread,
Feb 1, 2016, 9:00:45 PM2/1/16
to plutus-gem
I'm learning rails and I want to add this gem to my project.

  PettyCash < ActiveRecord::Base
    belongs_to :expense, class_name: "Plutus::Expense"
  end
  CashOnHand < ActiveRecord::Base
  belongs_to :asset, class_name: "Plutus::Asset"

How do I build the controller so that when I create a petty_cash, the amount would be subtracted from the CashOnHand? Thank you.

mbulat

unread,
Jun 9, 2016, 1:35:36 PM6/9/16
to plutus-gem
The idea would be to actually persist those accounts to the database as the account subtype. So instead you'd have the following:

Plutus::Expense.create(:name => "Petty Cash")
Plutus::Assett.create(:name => "Cash on Hand")


Then when you go to create an entry, you lookup those accounts and reference them with the debit or credit amounts. 

Since each record knows it's type, you can do the following:

cash = Plutus::Asset.find_by_name("Cash on Hand")
cash
.balance(:from_date => "2014-01-01", :to_date => Date.today)


and it will correctly total the credit/debit balances.

This way you can have a book of accounts with hundreds of account types without maintaining all those models in your code base.
Reply all
Reply to author
Forward
0 new messages