activerecord - rails active record group by for child class -


class goal   has_many :tasks end  class task   belongs_to :goal end 

for each goal, tasks grouped task attribute size. in view can display table shows number of tasks each size.

goal  size  count   sm    5   med   3   lg    4   xl    2  goal b  size  count   sm    10   med   7   lg    8   xl    0 

initially had table tasks , able achieve wanted with

task.group(:size).count.sort_by { |size, _| size } 

now have added parent class, goal, there way active query record tasks each goal , have tasks grouped size?

if goal has many tasks can goal.tasks.group(:size).count.sort_by { |size, _| size } goal might goal = goal.find_by(id: 1)


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -