mardi 30 juin 2015

Rails Mongoid how to get count of tasks for all users of a team?

I have the following schema in mongoid:

Team has many users.

User belongs to team and has many tasks.

Task belongs to user. 

I want to get ALL tasks for a certain team. How can I do it in a single access to DB?

The following query takes too much time:

team = Team.where(_id: params[:id]).first
tasks = 0
team.users.each{|user| tasks = tasks + user.tasks.count}

I would like to do a single access to DB. something like:

team.users.tasks.count

Aucun commentaire:

Enregistrer un commentaire