2012-06-19 55 views
1

这是我在控制器中的当前代码。如何在Rails中添加2个字符串变量计数?

@rating = (@approvals.count/@user.pages.count) + (@awards.count/@user.stories.count) 

当我打电话分开这些变量,他们拿出,因为他们应该的数字。当我将它们加在一起时,总和似乎是1或0.是否有某种方法将变量计数加在一起?

我已经看过其他职位,要么无法找到我所需要的,或我不理解它。

回答

2

这可能是因为你需要将它们转换为浮动。尝试使用to_f方法。

@rating = (@approvals.count.to_f/@user.pages.count) + (@awards.count.to_f/@user.stories.count) 
+0

天才。谢谢! – user1467317

+0

不客气。 –

+1

应该指出,这需要发生,因为只有部门,因为像'4/10'*整数*结果是'0'。 –

相关问题