Ruby on Rails 3 简单分组和计数
假设我有一个“汽车”对象,其中包含“颜色”列。在我看来,我想要一个颜色列表及其出现的次数。
---Car----Color---
Explorer Black
Charger Yellow
Prius Black
Jetta Black
Ferrari Red
Pinto Yellow
在我看来,我想要:
--Color--Count--
Black 3
Yellow 2
Red 1
在我的控制器中,我尝试制作一个类似的列表:
@colorcount = Car.all.count(:group => :color)
然后在我看来,我有类似的内容
<%= @colorcount.each do |car, count| %>
<%= car.color %>, <%= count %>
<% end %>
,但我收到了类似的错误:
undefined method `each' for 0:Fixnum
还有更多吗?感谢您的任何帮助。
Say I have a 'Car' object which contains a 'color' column. I want in my view a listing of color with the number of times it has appeared.
---Car----Color---
Explorer Black
Charger Yellow
Prius Black
Jetta Black
Ferrari Red
Pinto Yellow
In my view, I want:
--Color--Count--
Black 3
Yellow 2
Red 1
In my controller I tried making a list like:
@colorcount = Car.all.count(:group => :color)
and then in my view I have something like
<%= @colorcount.each do |car, count| %>
<%= car.color %>, <%= count %>
<% end %>
but I'm getting an error like:
undefined method `each' for 0:Fixnum
Is there a lot more to this? Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)