Hi everybody, I have a little problem: I can't compose sql-query inside AR.
So, I have Project
and Task
models, Project has_many Tasks
. Task
has aasm-field (i.e. "status"; but it doesn't matter, i can be simple int or string field).
So, I want on my projects index page list all (last) projects and for every project I want count it's active, pending and resolved (for example) tasks.
Like this, just look:
- First project (1 active, 2 pending, 10 resolved)
- Second projects (4 active, 2 pending, 2 resolved)
So, sure I can do it with @projects = Project.all
and then in view:
- @projects.each do |project|
= project.title
= project.tasks(:conditions => {:status => "active"}).count #sure it should be in model, just for example
= project.tasks(:conditions => {:status => "pending"}).count
# ...
- end
This is good, but makes 1+N*3 (for 3 task statuses) queries, i want 1. The question is simple: how?
.