Group and count in Rails
hash of devise_id as key and associated records count
@project.line_items.group(:device_id).count
Just add a :select
option:
@line_items = @project.line_items.all(
:group => "device_id",
:select => "device_id, COUNT(*) as count"
)
Then each @line_item
will have a count
attribute.
You can do count
on line_items
which will return you an ordered hash of device_id
and count
.
@project.line_items.group(:device_id).count