获取 Ruby On Rails 中属性的总和
我有 2 个模型,Agent
和 Currency
,具有以下关系。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照 Frankie 的建议进行操作,或者调用 object_result.to_i。
Do what Frankie suggests, or call object_result.to_i.