我有两个对象,访客和活动.参观者有多个活动.一个事件存储这样的参数......
#<Event id: 5466, event_type: "Visit", visitor_token: "c26a6098-64bb-4652-9aa0-e41c214f42cb", contact_id: 657, data: {"url"=>"http://widget.powerpress.co/", "title"=>"Home (light) | Widget"}, created_at: "2015-12-17 14:51:53", updated_at: "2015-12-17 14:51:53", website_id: 2>
Run Code Online (Sandbox Code Playgroud)
如您所见,有一个名为data的序列化文本列,用于存储包含更多数据的哈希.
我需要找出一个访问者是否访问过某个页面,如果url参数是它自己的列,或者如果哈希是一个hstore列,那么这将是非常简单的,但是它最初并没有这样设置它是一个保存的哈希的一部分.
这是我尝试过的rails查询...
visitor.events.where("data -> url = :value", value: 'http://widget.powerpress.co/')
visitor.events.where("data like ?", "{'url' => 'http://widget.powerpress.co/'}")
visitor.events.where("data -> :key LIKE :value", :key => 'url', :value => "%http://widget.powerpress.co/%")
Run Code Online (Sandbox Code Playgroud)
如何正确查询postgres以查找具有包含具有特定值的键的哈希的对象?
在我的Contacts类中,在使用他们的电子邮件地址创建联系人之后,我尝试尽可能多地从FullContact的API中提取联系人数据.
我遇到这个问题,如果FullContact中的"人"不存在一列数据,它会抛出NoMethodError而我无法保存可能存在于联系人中的其余数据,因为我的方法停在错误处.
我怎样才能从NoMethodError中解救并让我的方法继续运行其余的呢?喜欢它只是跳过错误并尝试其余的代码.我已尝试next并continue在我的救援代码中,但这不起作用.
谢谢你的帮助.
class Contact < ActiveRecord::Base
belongs_to :user
after_create do |contact|
contact.delay.update_fullcontact_data
end
def update_fullcontact_data
person = FullContact.person(self.email)
if person.contact_info.given_name.present?
self.name = person.contact_info.given_name
end
if person.contact_info.family_name.present?
self.last_name = person.contact_info.family_name
end
if person.demographics.location_general.present?
self.city = person.demographics.location_general
end
save!
rescue NoMethodError => exception
puts "Hit a NoMethodError"
save!
end
end
Run Code Online (Sandbox Code Playgroud) 我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有什么亮点。
归结为提出一些请求,但是我无法通过他们的身份验证步骤。
curl -X "GET" "https://api.sendgrid.com/v3/templates" -H "Authorization: Bearer Your.API.Key-HERE" -H "Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)
并使用 Rest-Client gem 我试图像这样发出身份验证请求......
username = 'username'
api_key = 'SG.MY_API_KEY'
key = Base64.encode64(username + ":" + api_key)
headers = {"Authorization" => "Bearer #{key}", "Content-Type" => "application/json"}
response = RestClient.get 'https://api.sendgrid.com/v3/templates', headers
Run Code Online (Sandbox Code Playgroud)
返回...
RestClient::Unauthorized: 401 Unauthorized: {"errors":[{"field":null,"message":"authorization required"}]}
Run Code Online (Sandbox Code Playgroud)
我如何错误地提出这个获取请求?
ruby ×3
activerecord ×1
hash ×1
mysql ×1
postgresql ×1
rescue ×1
rest ×1
rest-client ×1
sendgrid ×1