On Sunday, May 31, 2020 at 12:39:41 PM UTC-7, Aryk Grosz wrote:
>
> 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])
>

I'm assuming you have:

  Classroom.many_to_many :students
  Student.many_to_many :classrooms
  classroom_datasets = Classroom.dataset
  exclude_classroom_dataset = Classroom.where(:id=>1..100)
  student_dataset = Student.dataset

Then you can use association filters:
 
  
classroom_dataset.exclude(:students=>students_dataset.where(:classrooms=>exclude_classroom_dataset))

Are there cases where you think association_pks dataset methods would be 
needed that aren't covered by association filters?

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/95532162-23ee-44d5-bff1-06644495d737%40googlegroups.com.

Reply via email to