1

I have created this hash from scraped xml data I have retrieved from the web. My next task is to insert this data into a postgresql database where each key is a different column, and I have drawn a blank. Any help is appreciated. Thanks in advance.

temp = @ttc.css('vehicle').map do |vehicle|

{
    id: vehicle.attributes["id"].value,
    routeTag: vehicle.attributes["routeTag"].value,
    lat: vehicle.attributes["lat"].value,
    lon: vehicle.attributes["lon"].value
}

end
2
  • Postgres provide dedicated datatype for hash i.e. hstore. Commented Jul 23, 2015 at 13:40
  • if I understand correctly @ttc.css('vehicle') is an array of objects? Commented Jul 23, 2015 at 13:57

1 Answer 1

1

Provided your model name is Vehicle and your vehicles table has columns named id, routeTag, lat, lon, your solution would be as simple as this:

@new_vehicle = Vehicle.create(temp)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.