I am building a webapp using Rails 3.2.8.
In this app I got three tables. Projects, Tasks and Subtasks.
Projects
id, name
Tasks
id, name, project_id
Subtasks
id, name, desc, task_id, value
I would like to get all subtasks that belongs to a project and sum up the values and group them by created_at.
This is what I try but it does not work (only get a AR relation back).
@tasks = @project.tasks.joins(:subtasks).select('sum(subtasks.value)').group('subtasks.created_at')
What am I doing wrong?
Thankful for all help!
I think you’re closer than you realize.
Each of the methods in the chain you have provided result in
ActiveRecord::Relation. All you need to do is callalloreachor some other method to force it to execute the query.I think you also meant to group by
tasks.created_at?Finally, I’d recommend you created a class method in the
Taskmodel to simplify this a little.I hope that helps.