Created
January 27, 2011 04:52
-
-
Save mrjabba/798086 to your computer and use it in GitHub Desktop.
Find some users who are NOT in this group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ActiveRecord scoped query left outer join users to members | |
scope :not_in_group, lambda { |group| | |
join_clause = User.send(:sanitize_sql_array, | |
["LEFT OUTER JOIN memberships ON memberships.user_id = users.id WHERE memberships.group_id is not ?", group ]) | |
User.select("distinct(users.id), users.*").joins(join_clause) | |
} |
Ah the "who are not in a group at all." changes it quite a bit, so you will need the left join, you're quite right :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Heh. Would have helped for me to read your second comment too :-) No worries. I'm just happy that I'm finally getting past all the basic Rails/ActiveRecord stuff and now actually modelling some useful data.