Note that the accepted answer will return a hash:
Tagging.joins(:tag).group(:name).size
(0.4ms) SELECT COUNT(*) AS count_all, `name` AS name FROM `taggings` INNER JOIN `tags` ON `tags`.`id` = `taggings`.`tag_id` GROUP BY `name`
=> {"applesauce"=>1, "oranges"=>2}
But what if you want to return the count plus some columns from different tables in a join. Then you also need to use the select ActiveRecord query:
collection = Tagging.select('COUNT(*) as total, taggings.taggable_id, taggings.taggable_type').joins(:tag).where(taggable_type: 'LineItem', taggable_id: ['5cad0dcc3ed1496086000031', '5cad0dcd3ed1496086000081'] ).group(:name)
collection.each do |item|
puts item.taggable_id
puts item.total
end
5cad0dcc3ed1496086000031
1
5cad0dcc3ed1496086000031
2
With this second approach, you can fetch additional details about a join relationship without any additional queries or loop constructs.