股票多次交易的 Rails 建模

发布于 2024-12-10 06:16:04 字数 734 浏览 0 评论 0原文

我正在尝试在 Rails 中建模一个(非常)简单的股票交易模拟器来使用 3.1,并尝试了解更多有关 Web 后端的内容,而不是我通常坐的 UX。

因此,假设您有

Users (has many transactions)
Transactions (belongs to users, companies)
Companies (has many transactions)

一个用户购买了一家公司的一定数量的股票,并创建了一条交易记录,其中的数量列反映了为该特定交易购买的股票数量、当时的股价和状态指示物品是否被购买或出售的列。 (为简单起见,并且由于这是一项学习练习,我们假设 1 笔交易仅包含一次买入或卖出事件)

因此,假设用户出于某种原因买入和卖出同一家公司的各种奇怪数量。

他最终会得到多个交易行,必须对

这些交易行进行查询和求和,以便弄清楚他是否拥有该公司

  • 的任何股份以及
  • 他拥有多少(所有买入 - 卖出)
  • 等。

所以我的问题是,该模型适合这种类型的使用吗?例如,如果我想编写一个页面来指示用户拥有什么,这似乎很麻烦。这意味着运行各种金额来确定是否拥有以及拥有的数量。但也许这就是这些事情应该做的?或者有更好的方法吗?

只维护一个代表“所有权”的交易,并为每个“拥有”的公司更新其中的列,然后将各个交易记录在第二个分离出来并加入到交易中的 transaction_history 表中,这样会更好吗?

I'm trying to model a (very) simple share trading simulator in rails to play with 3.1 and try and learn more about web backend stuff as opposed to UX where I normally sit.

so lets say you've got

Users (has many transactions)
Transactions (belongs to users, companies)
Companies (has many transactions)

So a User buys a number of shares in a company, and that creates a transaction record with the quantity column reflecting the number of shares bought for that particular transaction, the share price at the time, and a status column indicating whether things were bought or sold. (For simplicities sake, and as this is a learning exercise, lets assume that 1 transaction contains only one buy OR sell event)

So lets say the user, for some reason, buys and sells all kinds of strange quantities of the same company.

He'll end up with multiple transaction rows, which must be queried and summed in order to figure out

for each company

  • if he owns any shares in it at all
  • how much he owns (all buys - sells)
  • etc.

So my question is, is this model correct for this type of use? It seems cumbersome if I want to, for example, write a page indicating what the user owns. It means running all kinds of sums of to determine if owned, and amount owned. But maybe thats just how these things should be done? Or is there a better way?

Would it be better to maintain only one transaction representing 'ownership', and update the columns in it for each company 'owned' and then record individual transactions in a second, transaction_history table that is separated out and joined to transaction?

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-12-17 06:16:04

我认为你的模型结构很好。如果您想简化用户的当前所有权,那么您可以添加一个附加表来有效缓存此信息。也许添加一个 OwnedShares 模型来引用用户、公司和所拥有的股份数量。

然后,您可以在交易模型中使用“after_save”回调来更新这些 OwnedShares。

通过这种结构,报告用户当前的所有权将很容易,但您不会丢失交易历史记录。

I think your model structure is fine. If you want to simplify the current ownership of a user, then you could add an additional table to effectively cache this information. Perhaps add a OwnedShares model that references a user, a company, and the amount of shares owned.

You could then use an "after_save" callback in the transactions model to update these OwnedShares.

With this structure, reporting on current ownership for a User would be easy, but you don't lose the transaction history.

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