Quick and dirty mass-insert with ActiveRecord and PostgreSQL. This gem assumes that all data being inserted has already been validated, and that table and column names have been checked for SQL injection vulnerabilities.
Add this line to your application's Gemfile:
gem 'bulk_insert'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bulk_insert
BulkInsert::Handler.new(table_name, [*column_names]).insert([*rows])
For example, to insert 10 comments into your database you can use the following...
BulkInsert::Handler.new("comments", ["id", "author_id", "text", "created_at"]).insert([
{"id" => 1, "author_id" => 1, "text" => "Hello world!", "created_at" => "now"},
...
])
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request