1

我的会话控制器中有以下代码,它将 Facebook 朋友、喜欢和用户个人资料等信息保存到我的数据库中。该配置文件包括用户的位置和性别,但它作为字符串保存到数据库中,因此我无法提取它。

@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

进入控制台,我可以通过以下方式获取最后一个用户的个人资料:

u = User.last.profile

但这并不允许我专门调用该位置,例如:

User.last.profile.location

用户表看起来像

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
4

4 回答 4

4

像这样?

graph = Koala::Facebook::API.new(@oauth_token)
@user = graph.get_object("me")
location = @user["location"]["name"]
于 2011-11-07T11:22:19.433 回答
2

我正在使用koala v2.2,我的应用程序 Facebook API 版本是v2.4. 我没有仅使用请求获得location信息。get_object("me")我必须location像这样的fields参数传递

get_object("me?fields=first_name,last_name,location")

可能这些信息会对某人有所帮助。

注意:当然,您必须启用user_location范围。

于 2015-09-16T10:52:22.620 回答
0

我用来解决这个问题的方法是使用范围并在视图中调用这些范围,这可能不是最好的方法,但可以快速解决。

用户模型的范围可以是:

scope :CityName, where("profile like '%location: CityName%'")
于 2011-08-25T05:42:25.797 回答
0

从昨天开始使用 Facebook Checkins,例如:

unless graph.get_connections('me', 'checkins', :since => 'yesterday').blank?
checkin = graph.get_connections('me', "checkins", :since => 'yesterday')
lat = checkin[0]['place']['location']['latitude']
long = checkin[0]['place']['location']['longitude']
于 2011-11-24T11:37:40.957 回答