ruby - ActiveRecord: peer models -


what best way relate models peers?

for example, consider classic banking example

class transaction < ar::base    belongs_to :account    # attribute: amount decimal end class account < ar::base    has_many :transactions    # attribute name string end # move money this: t1 = transaction.create(:amount=>10, :account=>account.find_by_name('mine')) t2 = transaction.create(:amount=>-10, :account=>account.find_by_name('yours')) 

i want relate 2 transactions can go particular deposit exact withdrawal opposite.

i add transaction model:

belongs_to :mirror_transaction, :class_name=>'transaction' has_one :other_transaction, :class_name=>'transaction', :foreign_key=>'mirror_transaction_id' 

... feels bit icky. can't express better that!

the other way can think of create third wrapper model, like:

class transactionset < ar::base    has_many :transactions end 

note cannot extend transaction model relate both accounts in one. payments go "outside system" i.e. not paired. also, in real-world problem have, model more complex , don't want double everything.

any advice or other ideas?

tia!

vouchers have debit , credit entries. vouchers need validate sum of debits , credits equal. voucher model encapsulates net transaction. in fact 2 transactions must built thru (internally) thru voucher (transactionset) model itself.

i sharing since i'm working on building bookkeeping/accounting system myself.

you may pass flags prevent validations balancing not kick off in case transaction's pair falls outside of system.

in real world, kind of pairing can between more 1 transaction entry. 1 debit pairs 2 credits example.

i know question more technical , apologies if i'm assuming need model solution around have identified transactionset rather call icky solution.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -