Catalin B

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Hello,

I need some help/advise on how to save third data in my database, also I'm interested in any tutorial or info about these topic.
I'm struggling to save data from third party api.
I get the result and I can display the result on my app. When I want to save data in my DB it saves only the id and the zone attribute.

Here is the response body

{
"from": 1,
"to": 100,
"total": 4490,
"auditData": {
"processTime": "157",
"timestamp": "2016-05-08 09:58:05.163",
"requestHost": "",
"serverId": "",
"environment": "[int]",
"release": ""
},
"destinations": [{
"code": "A1N",
"name": {
"content": "Ansan"
},
"countryCode": "KR",
"isoCode": "KR",
"zones": [{
"zoneCode": 1,
"name": "Ansan downtown"
}],
"groupZones": []
}, 
}

In my Destination model where I have the destinations_table

attr_accessor :destination_id, :code, :country_code, :name, :zones

def self.set_destinations
url ="https://api.test.hotelbeds.com/hotel-content-api/1.0/locations/destinations"
response = HTTParty.get(url,
:query => { 
"fields" => 'code,name,countryCode,zones',
"language" => "ENG",
"from" => "1",
"to" => "5", 
"useSecondaryLanguage" => "false" 
},
:headers => add_signature_for_hotels
)
result = JSON.parse(response.body)
 result["destinations"].each do |value|
   Destination.create!("destination_id" => value["id"], "code" => value["code"], "name" => value["name"],
        "country_code" => value["countryCode"], "zone" => value["zones"][0]["name"] )
 end
end

If I try in my console result[0]["code"] it gives my the result "A1N" but if I put in my method self.set_destinations it gives me an error. undefined method `[]' for nil:NilClass

At this moment only save the destination.id = 1 and zones["name"] = Ansan downtown, how can I save the data for destination_id, code, name, and country_code.
These is how looks my table after a request:

1   | null | null | null | null | Ansan downtown |  2016-05-07 20:19:34.546309 |    2016-05-07 20:19:34.546309

How can I save all the data in my DB?

Thanks,
Cata

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.