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
Post a Comment