# How to do the batch insert?

**URL:** <https://www.ruby-forum.com/t/how-to-do-the-batch-insert/122718>\
**Category:** Rails\
**Created:** [December 11, 2007, 9:10am UTC](https://www.ruby-forum.com/t/how-to-do-the-batch-insert/122718 "2007-12-11T09:10:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayyanar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@ayyanar](https://www.ruby-forum.com/u/ayyanar)\
**Post date:** [December 11, 2007, 9:10am UTC](https://www.ruby-forum.com/t/how-to-do-the-batch-insert/122718/1 "2007-12-11T09:10:14Z")

</div>

The below code deletes all the task users and inserts the new task users  
by getting the ids from the UI.

I have two questions:

1. Is there any way to delete all the records in a single statement.  
If we put task\_users = [], generating the delete statement for every  
record

2. Is there any way to do the batch insert? The below code generates  
more insert statement

---

<div class="post-metadata">

**Author:** ![ayyanar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@ayyanar](https://www.ruby-forum.com/u/ayyanar)\
**Post date:** [December 11, 2007, 9:36am UTC](https://www.ruby-forum.com/t/how-to-do-the-batch-insert/122718/2 "2007-12-11T09:36:28Z")

</div>

One correction in the 1st question

1. Is there any way to delete all the records in a single statement.  
The below code @task.task\_users = task\_users deletes the task\_users  
and insert the tasks users. Assume there are 4 existing task users  
and 2 new task users. This will generate 4 delete statements and  
2 insert statements. I expect single statement to delete all the 4  
records  
and single statement to insert all the two records.
