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
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