Skip to content

Instantly share code, notes, and snippets.

@jcutrell
Created October 18, 2013 18:47
Show Gist options
  • Save jcutrell/7046200 to your computer and use it in GitHub Desktop.
Save jcutrell/7046200 to your computer and use it in GitHub Desktop.
The goal of the query is to select all users who do not have assignments within a range. The range is defined on the assignment model, so this would be in assignment.rb in a rails project Startdatetime and enddatetime are attributes on the Assignment model.
User.joins(:assignments).where.not("(assignments.startdatetime > ? AND assignments.startdatetime < ?) OR (assignments.enddatetime < ? AND assignments.enddatetime > ?)", startdatetime, enddatetime, startdatetime, enddatetime)
# This would be fine, except it does not include users who do not have assignments at all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment