ActiveRecord:对等模型

发布于 2024-10-18 04:35:39 字数 1014 浏览 3 评论 0原文

将模型关联为对等模型的最佳方式是什么?

例如,考虑经典的银行业务示例,

class Transaction < AR::Base
   belongs_to :account
   # attribute: amount decimal
end
class Account < AR::Base
   has_many :transactions
   # attribute name string
end
# move money like this:
t1 = Transaction.create(:amount=>10, :account=>Account.find_by_name('Mine'))
t2 = Transaction.create(:amount=>-10, :account=>Account.find_by_name('Yours'))

我想要将两笔交易关联起来,以便我可以从特定的存款转到与其相反的确切取款。

我可以将其添加到 Transaction 模型中:

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

...但感觉有点恶心。我无法表达比这更好的了!

我能想到的唯一另一种方法是创建第三个包装器模型,例如:

class TransactionSet < AR::Base
   has_many :transactions
end

请注意,我不能简单地扩展我的事务模型以与一个帐户中的两个帐户相关。有些付款会在“系统之外”进行,即它们不会配对。另外,在我遇到的现实问题中,模型要复杂得多,我不想将所有内容加倍。

有什么建议或其他想法吗?

蒂亚!

What is the best way to relate models as peers?

For example, consider the 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 like 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 to relate the two transactions so I can go from a particular deposit to the exact withdrawal that is its opposite.

I could add this to the Transaction model:

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

... but it feels a bit icky. I can't express it any better than that!

The only other way I can think of is to create a third wrapper model, something like:

class TransactionSet < AR::Base
   has_many :transactions
end

Note that I cannot simply extend my Transaction model to relate to both Accounts in one. Some payments go "outside the system" i.e. they will not be paired. Also, in the real-world problem I have, the model is much more complex and I don't want to double everything.

Any advice or other ideas?

TIA!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-10-25 04:35:39

优惠券有借方和贷方条目。优惠券还需要验证借方和贷方的总和是否相等。凭证模型封装了净交易。事实上,您的 2 个交易必须通过凭证(TransactionSet)模型本身(内部)构建。

我与您分享这个是因为我正在自己构建一个簿记/会计系统。

您可以传递标志来防止平衡验证在某些交易对落在系统之外的情况下不会启动。

在现实世界中,这种配对可以发生在多个交易条目之间。例如,一对借方与 2 个贷方对。

我知道你的问题更具技术性,如果我假设太多,我很抱歉,但你确实需要围绕你所确定的 TransactionSet 而不是你所说的令人讨厌的解决方案来建模你的解决方案。

Vouchers have debit and credit entries. Vouchers also need to be validate that the sum of debits and credits is equal. Voucher Model encapsulates the net transaction. In fact your 2 transactions must be built thru (internally) thru the Voucher (TransactionSet) model itself.

I sharing this with you since i'm working on building a bookkeeping/accounting system myself.

You may pass flags to prevent validations for balancing to not kick off in case certain transaction's pair falls outside of the system.

In the real world, this kind of pairing can be between more than one transaction entry. One debit pairs with 2 credits for example.

I know your question is more technical and apologies if i'm assuming too much but you really need to model your solution around what you have identified as the TransactionSet rather than what you call your icky solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文