如何获取特定时间段内的结果计数?
我有一个屏幕,上面有我们的订单,我想显示一些计数以便快速、轻松地报告。
我已经发现我可以通过以下方式获得所有订单的计数
<%= Order.count(:all) %>
我还想显示特定日期范围内的一些计数,例如“今天”、“昨天”、“本周”、“本月”、“最后” 我对 Rails 还很陌生,最近才从 PHP 跳出来,
所以我仍然在习惯 Rails 的做事方式。我们使用 Rails 3 和 ActiveRecord 来查询数据库。实现这一目标的最佳方法是什么?
谢谢!
I have a screen with our orders on it and I want to show some counts for quick and easy reporting.
I have worked out that I can get a count of all orders by doing
<%= Order.count(:all) %>
I would also like to show some counts from specific date ranges, for example 'today', 'yesterday', 'this week', 'this month', 'last month, 'this year' etc.
I'm very new to rails, having recently made the leap from PHP so I'm still getting used to the Rails way of doing things. We're using Rails 3 and ActiveRecord to query the DB. What would be the best way to go about achieving this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看这个例子。您可以将条件传递给 count 方法。
更多API 文档中的示例
Look at this example. You can pass in conditions to the count method.
More examples from the API documentation
首先,您不应该在视图中使用查找器(或任何逻辑)。相反,将 count 作为订单模型的类方法,或将其设为范围。此外,在 Rails 3 上,最好使用新语法(与您的计数相同):
现在,您可以使用 Rails 模型的created_at 属性来执行您想要的操作。然后,您只需要执行以下操作:
First of all, you should not use finders(or any logic) inside your views. Instead, put count as a class method for your order model, or make it a scope. Moreover, on Rails 3, it's better to use the new syntax (same for your count) :
Now, you can use the created_at attribute of Rails models to do what you want. Then, you would just need to do something like :