D
D
Daniil Laptev2014-02-08 17:27:53
Ruby on Rails
Daniil Laptev, 2014-02-08 17:27:53

Sampling tasks by the number of performers?

Good evening.
There is a table tasks with tasks, it has a field :members_count - the maximum number of performers for one task. The performers are stored in the task_users table , which contains the :task_id and :user_id fields.
How to select only those tasks whose number of performers is less than :members_count?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
himik, 2014-02-08
@himik

users_count = TaskUser.select("DISTINCT(user_id)").count
Task.where("members_count > ?", users_count)

as one of the options, since you did not show the models.

T
Tonis Simo, 2014-02-10
@estum

With gem squeel something like this:

Task.joins{task_users}.group{tasks.id}.having{count(task_users.id) < tasks.members_count}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question