Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
SQL scripts are not intended for serialization at all. Unless you need to insert your data in a database. Is this the case? –  Stefano Sanfilippo Jan 12 at 17:07
    
I want convert my Array of objects into different scripts and JSON. I have this array of objects and I want publish them, so anyone can download it and insert this data into their databases. For easily import this data, I think in JSON and SQL scripts that insert things on database. Have another way or am I right - this is the best way to publish and share my database? –  Fernando Paladini Jan 12 at 17:12
add comment

1 Answer

up vote 0 down vote accepted

After search a lot over the Internet and forums, I found "models_to_sql" gem, from Martin Provencher.

But this gem only works for Rails 3.2 and older, and my application uses Rails 4.0. I talked with author and he doesn't want support this gem anymore. So I discovered by myself how to support Rails 4.0 and now I'm maintaining this gem.

Download the "models-to-sql-rails" gem here, supporting Rails 4.0 and older.


With this gem, you can easily do the following. (the examples inside values are just a joke, you will get the correct values when using it in your object).

For objects:

object.to_sql_insert
# INSERT INTO modelName (field1, field2) VALUES ('Wow, amaze gem', 'much doge')

For array of objets:

array_of_objects.to_sql_insert
# INSERT INTO modelName (field1, field2) VALUES ('Awesome doge', "im fucking cop")
# INSERT INTO modelName (field1, field2) VALUES ('much profit', 'much doge')
# (...)

Just see the Github of this project and you'll find how to install and use this wonderful gem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.