将数据从 JSON api 保存到 Rails 数据库上的 Ruby

Saving Data from JSON api to Ruby on Rails DB

我在将从 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。当 map 在没有块的情况下被调用时,它 returns 一个枚举器 (https://ruby-doc.org/core-2.2.0/Array.html#method-i-map).

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

为了执行您的代码,删除 .first 就可以了;)