2016-04-02 7 views
1

私はRoRアプリケーションでChartkickを使用していて、GroupにあるPeopleのすべてを表示する円グラフを作成しようとしています。私のグループと個人モデルは両方ともHABTMです。アクティブなレコードを使って各グループの人数を把握する方法を知っていれば、チャートは現在動作しており、グループ数(スクリーンショットとコードは下記)のみ表示しています。ここ アクティブレコード - グループ内の人数をグラフ化していますか?

は、ここに私のコード

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

ある

ここenter image description here

はここに私のスキーマ

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 

あるスクリーンショットに人物モデル

されています10
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 

答えて

1
<%= pie_chart Group.includes(:people).all.map {|g| [g.name, g.people.size] }.to_h %> 

あり、それはより良いモデルの体にこのロジックを移動します。

+0

私はあなたが正しい軌道に乗っていると思うが、このエラーが発生する – Bitwise

+0

SQLite3 :: SQLException: "グループ"の近く:構文エラー:SELECT COUNT(*)AS count_all、グループASグループfrom "people" GROUP BY "group " – Bitwise

+0

あなたの質問をモデルとスキーマコードでお知らせください) – Ilya

関連する問題