Hello there,
I have two objects as follows:
BankAccount
int id
float amount
Transaction transactions[]
Transaction
int id
BankAccount account_credit_to # in the db table account_credit_to
is int
BankAccount account_debit_from # int in db table
float amount
date_time
Each transaction is associated with exactly 2 accounts.
How do I map these as Ruby models and access the objects.
Here is my attempt:
def BankAccount
has_many :transactions
end
def Transaction
#*** here how do I tell that this transaction references two
accounts *** ?
end
And how do I access them in the controller?
ba = BankAccount.new
ba.transactions???
Thanks in Advance!
MS
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.
On 5 May 2008, at 21:06, AndyV wrote:
- current_balance is a method that sums credits, debits and uses them
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
On 5/5/08, marahddis [email protected] wrote:
def Transaction
Thanks in Advance!
MS
–
Trevor S.
http://somethinglearned.com
Fred,
yarr - good catch. Fingers clearly moving faster than brain.
Trev
On 5/5/08, Frederick C. [email protected] wrote:
Shouldn’t that be :foreign_key => ‘account_debit_from_id’ (:as creates
a polymorphic association which i don’t think is required here)
Fred
–
Trevor S.
http://somethinglearned.com
Right, we actually use FinancialTransaction.
On May 5, 4:30 pm, Frederick C. [email protected]
On 5 May 2008, at 21:47, Trevor S. wrote:
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