gpt4 book ai didi

ruby-on-rails - Foursquare 位置哈希存储作为 Rails 模型属性/列

转载 作者:行者123 更新时间:2023-12-04 07:29:45 25 4
gpt4 key购买 nike

我正在使用 Foursquare2 Ruby 包装器调用 Foursquare API https://github.com/mattmueller/foursquare2

如果我在 Rails 控制台中初始化客户端:

client = Foursquare2::Client.new(:client_id =>'CLIENT_ID', :client_secret => 'CLIENT_SECRET')

然后我通过 location_id 调用 Foursquare API(例如使用 5104)。

restaurant = client.venue(5104)

这将返回一个巨大的 Ruby Hashie::Mash。因此,如果我只想要位置哈希,那么我会这样做

restaurant.location

这给了我这个“位置哈希”

#<Hashie::Mash address="4 Clinton St." cc="US" city="New York" country="United States" crossStreet="at E Houston St." lat=40.721294 lng=-73.983994 postalCode="10002" state="NY"> 

现在如果我做 restaurant.location.address这将返回 => "4 Clinton St."

现在,退出 Rails 控制台并进入 Rails 应用程序。我的问题 - 作为我的 Rails“餐厅”模型中的 after_save 方法调用,我将这个“位置哈希”保存在我的模型中的“foursquare_location”列中作为 :text 字段。

在显示餐厅地址详细信息的“餐厅” View 模板中,如何编写模板以便调用@restaurant.foursquare_location.address 以便它访问地址键以返回地址值?

我不确定我是否可以直接将 Foursquare API 响应直接保存到我的 SQLite3 数据库中的 :text 列中并让它保持其哈希结构。

非常感谢这里的帮助。

最佳答案

您可以使用 ActiveRecord serialize 方法:

class Restaurant < ActiveRecord::Base
serialize :foursquare_location
end

然后您可以将对象分配为 foursquare_location 的值。

这应该适用于 Hashie::Mash(我自己从未尝试过,但它适用于常规哈希)。如果您同时访问大量记录,您的性能可能会受到轻微影响,并且查询该字段会有点困难,但它应该适用于您所描述的内容。

关于ruby-on-rails - Foursquare 位置哈希存储作为 Rails 模型属性/列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com