Answer the question
In order to leave comments, you need to log in
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
users_count = TaskUser.select("DISTINCT(user_id)").count
Task.where("members_count > ?", users_count)
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 questionAsk a Question
731 491 924 answers to any question