In the simplest use case,  lets say you have a table classroom_students. 
You have a dataset of classrooms and you want to count the students in 
those classrooms.

So you join on the classroom_students table, and then do a group by 
classroom_id. However, you have another dataset of classrooms that that you 
want to exclude from the calculation, but you need the students from those 
classrooms

The most direct way of thinking about this is to grab all the student IDs 
in those classrooms and exclude them so:

>> exclude_students = 
exclude_classrooms_dataset.association_join(:classroom_students).select(:student_id).distinct

or more cleaner:

>> exclude_students = exclude_classrooms_dataset.student_pks_dataset (which 
automatically distinct and get it off the join table)

Now you can return the exclude_students count with exclude_students.count 
(since it's already distinct). You could also do:

classroom_dataset.association_join(:classroom_students).exclude(student_id: 
exclude_students).group(Sequel[:classrooms][:id])

It's easier to reason about it this way instead of joining in a temp table, 
etc.

On Sunday, May 31, 2020 at 6:25:31 PM UTC+3, Jeremy Evans wrote:
>
> On Sunday, May 31, 2020 at 7:27:14 AM UTC-7, Aryk Grosz wrote:
>>
>> Thanks!
>>
>> Also, had an additional idea to create a dataset method as well, so you 
>> could do:
>>
>> school.users_dataset.book_pks_dataset and it would automatically put a 
>> distinct on it by default and return all those book_pks into a query that 
>> you could reuse.
>>
>
> Can you provide an example use case for this?
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/211158d5-5ce3-4943-96db-b6d96a0f1d46%40googlegroups.com.

Reply via email to