I made a Rake Task to parse information from some websites (using Nokogiri gem) and save everything to a .json file. To do this, I store all objects inside and array and before the rake task ends, I call a method to serialize the array of objects to an JSON file.
Currently, I'm doing something like:
# Declaring a global variable inside the 'namespace'
@all_objects = []
# Each website I want parse is added to '@all_objects' array
this_object = Object.new(:attr1 => variable1, :attr2 => variable2)
@all_objects << this_object
# After run the code above for all websites, I save them in .json file
File.open("public/data.json", "w") {
|f| f.write(@all_objects.to_json)
}
My question is:
What is the easist way to do the same thing, but serializing to an insert SQL script? Exist some method like myArray.to_postgresql
or myArray.to_mysql
, that get all objects of this array and generate a script to insert this data in a SQL database(INSERT into...)?
Since now, thanks!