Active Record - 绘制一个组中有多少人?

Active Record - Charting how many people in a group?

我在我的 RoR 应用程序中使用 Chartkick,我正在尝试创建一个饼图来显示每个 Group 中的所有 People。我的 Group 和 Person 模型都是 HABTM。现在图表正在运行并且只显示组数(下面的屏幕截图和代码)如果有人知道如何使用活动记录获取每个组中的人数我会喜欢它!

这是我的代码

<%= pie_chart Group.group(:name).count %>

这是截图

这是我的架构

  create_table "people", force: :cascade do |t|
t.string   "phone_number"
t.datetime "created_at",                  null: false
t.datetime "updated_at",                  null: false
t.boolean  "subscribed",   default: true, null: false
t.string   "city"
t.string   "state"
t.string   "zip"
t.string   "country"
 end

 create_table "groups", force: :cascade do |t|
t.string   "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

 create_table "groups_people", id: false, force: :cascade do |t|
t.integer "group_id",  null: false
t.integer "person_id", null: false
end

这里是人物模型

class Person < ActiveRecord::Base
has_many :deliveries
has_and_belongs_to_many :groups

这里是群模型

class Group < ActiveRecord::Base
 has_and_belongs_to_many :people
 has_and_belongs_to_many :messages
end
<%= pie_chart Group.includes(:people).all.map {|g| [g.name, g.people.size] }.to_h %>

对了,把这个逻辑移到model body里会更好