Rails 中的多列分组和总计
我的数据看起来像这样
___________________________________
|name date status location |
|Tom 1/1/2010 Ready Home |
|Tom 1/1/2010 Ready Home |
|Bill 1/18/2010 Go Work |
|Bob 2/19/2010 Ready Field |
|Bob 2/19/2010 Ready Field |
|Wil 4/5/2010 Steady Work |
|Wil 4/5/2010 Steady Work |
|Wil 4/5/2010 Steady Work |
|Wil 4/5/2010 Steady Home |
|Bill 7/14/2010 Stop Home |
|___________________________________|
我需要对其进行分组/计数,以便看起来像这样
Name Date Location Status Count
Tom 1/1/2010 Home Ready 2
Bill 1/18/2010 Work Go 1
Bill 7/14/2010 Home Stop 1
Bob 2/19/2010 Field Ready 2
etc.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时,根据数据的格式和属性,执行此操作的最佳方法很棘手。但一般来说,您可以使用 :group 参数“nofollow">ActiveRecord#find。
或者,如果使用 Rails 3
如果您始终打算根据此关系对数据进行分组,请考虑将其添加为模型的默认范围。
Sometimes the best way to do this is tricky based upon the format and properties of your data. But in general, you can use the
:group
argument of ActiveRecord#find.Or if using Rails 3
If you always intend to group the data based on this relationship, consider adding it as a default scope to your model.