获取 Ruby On Rails 中属性的总和

发布于 2024-11-17 15:35:44 字数 495 浏览 6 评论 0原文

我有 2 个模型,AgentCurrency ,具有以下关系。

Agent has_many :currencies

Currency belongs_to :agent

我有以下代码。

agent = Agent.find(1)

agentcurrs = agent.currencies

Currency 有一个名为“val”的属性。

我想要 agentcurrs 记录集的“val”总和,因此我执行以下操作:

totalval = agentcurrs.sum('val')

返回的是 BigDecimal 而不是值。

我做错了什么?我正在使用 Rails 2.3.8 和 Ruby 1.8。

I have 2 models, Agent and Currency with the following relationship.

Agent has_many :currencies

Currency belongs_to :agent

I have the following code.

agent = Agent.find(1)

agentcurrs = agent.currencies

Currency has an attribute called 'val'.

I want the sum of 'val' for the agentcurrs recordset so I'm doing the following:

totalval = agentcurrs.sum('val')

A BigDecimal is being returned rather than the value.

What am I doing wrong? I'm using Rails 2.3.8 and Ruby 1.8.

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

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

发布评论

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

评论(1

把时间冻结 2024-11-24 15:35:44

按照 Frankie 的建议进行操作,或者调用 object_result.to_i。

Do what Frankie suggests, or call object_result.to_i.

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