Skip to content

Instantly share code, notes, and snippets.

@whytrall
Created June 27, 2018 10:54
Show Gist options
  • Save whytrall/e373984ee457c95bc855421c4448b72d to your computer and use it in GitHub Desktop.
Save whytrall/e373984ee457c95bc855421c4448b72d to your computer and use it in GitHub Desktop.
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