简体   繁体   中英

How to perform this query in RoR

I have a rails active record query which returns a count of the the number of items in each category. In the form

Category.joins(:item).group("category_id").count
=> {1=>1, 2=>3}

which gives the correct result. I'm having an issue including the category name in the result along with item count. How do I include category name eg.

 1, Severe => 1, 
 2, Minor => 3 

Thanks!

试试这个

Category.joins(:item).group("categories.name").count

You could do:

Category.joins(:item).group([:category_id, :category_name]).count

Then you would get something like below:

{[1, "Severe"]=>1, [2, "Minor"]=>3}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM