Created
June 27, 2018 10:54
-
-
Save whytrall/e373984ee457c95bc855421c4448b72d to your computer and use it in GitHub Desktop.
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
def self.with_info(current_user, return_only_this_user = false) | |
sum = "SUM(CASE space_users.user_id WHEN #{current_user.id} THEN 1 ELSE 0 END)" | |
q = left_outer_joins(:space_users).distinct.select("spaces.*, COUNT(space_users.*) AS space_users_count, #{sum} AS current_user_is_in_space, space_users.created_at AS joined_at").group('spaces.id, space_users.id') | |
if return_only_this_user | |
q.having("#{sum} = 1") | |
else | |
q | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment