belongs_to :2accounts ... :3accounts ... :xaccounts ??

Hello there,

I have two objects as follows:

BankAccount

FWIW, we've added another layer (actually several but one it boils down to one for the area you're dealing with). It looks something like this:

Account   has_many :account_details -opening_balance -reconciled_balance -reconciled_on - current_balance is a method that sums credits, debits and uses them to offset the reconciled balance

AccountDetail   belongs_to Account   belongs_to Transaction   - detail_type (=Debit or Credit)   - amount

Transaction   has_many :account_details

Beyond the modeling, the main thing you need to do (surprise!) is make sure the debits and credits associated with the Transaction balance.

FWIW, we've added another layer (actually several but one it boils down to one for the area you're dealing with). It looks something like this:

Account has_many :account_details -opening_balance -reconciled_balance -reconciled_on - current_balance is a method that sums credits, debits and uses them to offset the reconciled balance

AccountDetail belongs_to Account belongs_to Transaction - detail_type (=Debit or Credit) - amount

Transaction has_many :account_details

Beyond the modeling, the main thing you need to do (surprise!) is make sure the debits and credits associated with the Transaction balance.

And also don't use the word transaction. You're asking for trouble.

Fred.

Hey,

to answer your specific question:

class Transaction < ActiveRecord::Base   belongs_to : account_credit_to, :class_name => 'BankAccount'   belongs_to :account_debit_from, :class_name => 'BankAccount' end

# Note - transactions table must have 'account_credit_to_id' and 'account_debit_from' integer columns.

class BankAccount   has_many :credit_transactions, :as => :account_credit_to, :class_name => 'Transaction'   has_many :debit_transactions, :as => :account_debit_from:, class_name => 'Transaction' end

HOWEVER... I would strongly advise against this code:

First of all, you might as well treat 'transaction' and 'Transaction' as reserved words in Rails. AccountTransaction or something similar will help to avoid conflicts.

Next, I don't know the details of the system you're trying to create but your table definition seems oversimplified for tracking the movement of money.

I think you probably should talk to the accountant that's going to eventually have to deal with these records you're creating. Find out the way they want you to record this information in a database (or how they'd do it with a paper journal).

Regards, Trevor

Hey,

to answer your specific question:

class Transaction < ActiveRecord::Base belongs_to : account_credit_to, :class_name => 'BankAccount' belongs_to :account_debit_from, :class_name => 'BankAccount' end

# Note - transactions table must have 'account_credit_to_id' and 'account_debit_from' integer columns.

class BankAccount has_many :credit_transactions, :as => :account_credit_to, :class_name => 'Transaction' has_many :debit_transactions, :as => :account_debit_from:, class_name => 'Transaction' end

Shouldn't that be :foreign_key => 'account_debit_from_id' (:as creates a polymorphic association which i don't think is required here)

Fred

Fred,

yarr - good catch. Fingers clearly moving faster than brain.

Trev

Right, we actually use FinancialTransaction.