I want to do a batch insert of few thousand records into the database (POSTGRES in my case) from within my Rails App.
What would be the \"Rails way\" of doing it? So
You can create a script in your rails model, write your queries to insert in that script In rails you can run the script using
rails runner MyModelName.my_method_name
Is the best way that i used in my project.
Update:
I use following in my project but it is not proper for sql injection. if you are not using user input in this query it may work for you
user_string = " ('a@ao.in','a'), ('b@ao.in','b')"
User.connection.insert("INSERT INTO users (email, name) VALUES"+user_string)
For Multiple records:
new_records = [
{:column => 'value', :column2 => 'value'},
{:column => 'value', :column2 => 'value'}
]
MyModel.create(new_records)