Last Updated: October 14, 2020
·
1.697K
· Tamer Shlash

Check whether a Facebook user is member of a group

EDIT: This protip uses FQL, which is deprecated in Facebook Platform v2.1 and above, so don't rely on it anymore.

You might need to use the Koala rubygem to check whether an Facebook user is a member of a certain group.

The following snippet contains a method which receives two parameters:

  • a user object which has has an accessor oauth_token that gets the user's access token and a uid accessor that gets the user's Facebook uid
  • and a gid which represents the Facebook group id.

The method will check whether the user has the necessary user_groups permission (if not, you should specify what to do yourself). Then it will perform a simple FQL Query that will eventually return whether the user is actually a member or not.

def is_group_member? user, gid
  facebook ||= Koala::Facebook::API.new(user.oauth_token)
  unless facebook.get_connections('me','permissions')[0]['user_groups']
    # Do something you want, like throwing an exception or returning false
  end
  query_string = "SELECT uid FROM group_member WHERE uid=#{user.uid} AND gid=#{gid}"
  (facebook.fql_query(query_string).count > 0)? true: false
end