我目前在我的控制器中有这个:
@softwares = Software.find_by_sql
(
"SELECT s.id, s.vendor, s.title, s.edition, SUM(l.amount) AS amount FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.vendor, s.title, s.edition"
)
它应该做什么,但数字是7.0或6.0而不是7或6 ..如何删除小数?在我看来,我确实只有<%= l.amount %>
答案 0 :(得分:2)
我宁愿控制演示文稿来查看。而不是
<%= l.amount %>
放:
<%= number_to_currency l.amount, :precision => 0 %>
这个辅助方法描述为here
另外,将SQL放入模型可能是更好的设计。然后,Controller执行@softwares = Software.all_with_licenses_amounts
答案 1 :(得分:0)
它以小数形式出现,因为l.amount是小数
尝试
"SELECT s.id, s.vendor, s.title, s.edition, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.vendor, s.title, s.edition"