Search code examples
ruby-on-railsruby-on-rails-3facebook-graph-apikoala

How do I get a user's location using Koala in rails?


I have the following code in my sessions controller which saves information such as Facebook friends, likes, and a user profile to my DB. The profile includes the user's location and gender but it gets saved into the DB as a string so I can't extract it.

@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save

Going into the console, I can get the profile of the last user via:

u = User.last.profile

But this doesn't let me call for the location specifically, like:

User.last.profile.location

User table looks like

create_table "users", :force => true do |t|
t.string   "provider"
t.string   "uid"
t.string   "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "token"
t.string   "likes"
t.string   "profile"
t.string   "location"
t.string   "interests"
t.string   "birthday"
t.string   "activities"
t.string   "friends"
end

Solution

  • like this?

    graph = Koala::Facebook::API.new(@oauth_token)
    @user = graph.get_object("me")
    location = @user["location"]["name"]