I think what you want here is `where("status = 'Released' OR (status = 'Graded' AND NOT assignments.release_necessary)")