【问题标题】:Saving Data from JSON api to Ruby on Rails DB将数据从 JSON api 保存到 Ruby on Rails DB
【发布时间】:2018-04-04 18:11:36
【问题描述】:

我在将从 Zendesk 的 API 导入的数据保存到 rails 数据库时遇到了惊人的困难。我正在使用以下代码来迭代和创建用户;虽然数据存在于文件中,但没有保存任何内容。这是我在我的用户模型中用来创建记录的代码,当我在控制台中运行User.save_data_from_api时,这些记录不起作用:

class User < ApplicationRecord

def self.save_data_from_api
uri = URI.parse("https://techcompany.zendesk.com/api/v2/users.json")
request = Net::HTTP::Get.new(uri)
request.basic_auth("adminname@blah.com", "properpassword")

req_options = {
use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
info = response.body
user_data = JSON.parse(info)

users = user_data.map.first do |line|
    u = User.new
    u.zendesk_id = line['users']['id']
    u.save
    u
end

end

end

这是我正在使用的 JSON 文件的示例:

    ["users", [{"id"=>333653850, 
    "url"=>"https://randomtechco.zendesk.com/api/v2/users/333653850.json", 
    "name"=>"Randy Buckles", "email"=>"rbuckles@randotechco.com", 
    "created_at"=>"2014-08-06T14:31:24Z", "updated_at"=>"2018-04- 
    04T14:22:06Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>nil, 
    "shared_phone_number"=>nil, "photo"=> 
 {"url"=>"https://randomtechco.zendesk.com/api/v2/attachments/68955389.jso  ", "id"=>68955389, "file_name"=>"Work.jpg", "content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work.jpg", "mapped_content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work.jpg", "content_type"=>"image/jpeg", "size"=>2528, "width"=>80, "height"=>80, "inline"=>false, "thumbnails"=> 

 [{"url"=>"https://randomtechco.zendesk.com/api/v2/attachments/68955399.json", "id"=>68955399, "file_name"=>"Work_thumb.jpg", "content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work_thumb.jpg", "mapped_content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work_thumb.jpg", "content_type"=>"image/jpeg", "size"=>2522, "width"=>32, "height"=>32, "inline"=>false}]}, "locale_id"=>1, "locale"=>"en-US", "organization_id"=>nil, "role"=>"admin", "verified"=>true, "external_id"=>nil, "tags"=>[], "alias"=>"", "active"=>true, "shared"=>false, "shared_agent"=>false, "last_login_at"=>"2018-04-04T14:21:44Z", "two_factor_auth_enabled"=>nil, "signature"=>"Thanks for contacting the helpdesk!\n-Randy", "details"=>"", "notes"=>"", "role_type"=>nil, "custom_role_id"=>nil, "moderator"=>true, "ticket_restriction"=>nil, "only_private_comments"=>false, "restricted_agent"=>false, "suspended"=>false, "chat_only"=>false, "default_group_id"=>21692179, "user_fields"=>{}}, {"id"=>333666109, "url"=>"https://randomtechco.zendesk.com/api/v2/users/333666100.json", "name"=>"Caller +1 (800) 446-9635", "email"=>nil, "created_at"=>"2014-08-06T14:56:02Z", "updated_at"=>"2014-08-06T14:56:02Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>"+18004469635", "shared_phone_number"=>false, "photo"=>nil, "locale_id"=>1, "locale"=>"en-US", "organization_id"=>nil, "role"=>"end-user", "verified"=>true, "external_id"=>nil, "tags"=>[], "alias"=>nil, "active"=>true, "shared"=>false, "shared_agent"=>false, "last_login_at"=>nil, "two_factor_auth_enabled"=>false, "signature"=>nil, "details"=>nil, "notes"=>nil, "role_type"=>nil, "custom_role_id"=>nil, "moderator"=>false, "ticket_restriction"=>"requested", "only_private_comments"=>false, "restricted_agent"=>true, "suspended"=>false, "chat_only"=>false, "default_group_id"=>nil, "user_fields"=>{}}

【问题讨论】:

  • 你为什么把 map.first 放在首位?
  • 没有它我收到了这个错误:TypeError: no implicit conversion of String into Integer

标签: ruby-on-rails json database import zendesk-api


【解决方案1】:

问题是map.first。当 map 在没有块的情况下被调用时,它会返回一个枚举器 (https://ruby-doc.org/core-2.2.0/Array.html#method-i-map)。

因此,当您在map 返回的枚举器上调用.first 时,它会返回枚举器的第一个元素并且 该块被简单地忽略,因为first 不期望一个块。

为了使您的代码执行,请删除.first,然后您就可以开始使用了;)

【讨论】:

  • 谢谢克里斯蒂亚诺!删除 .first 时,出现以下错误:TypeError: no implicit conversion of String into Integer 你对我遗漏的内容有什么建议吗?再次感谢!
  • 您的数据库主键应该是一个整数,而来自 line['users']['id'] 的内容似乎是一个字符串。如果对您有帮助,请点赞我的回答;)
  • 行['users']['id'].to_i ?也许
  • 嗨,克里斯蒂亚诺,我还没有达到要求的十五个,所以我的赞成票还没有显示。 :-)
  • 所以将我的答案标记为已选择的答案,因为它解决了您的问题。
猜你喜欢
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多