How to do the batch insert?

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

    @task = Task.find(params[:task_id])

    task_users = []

    params[:ids].split(", ").each do |court_user_id|
    task_users << TaskUser.new(:court_user_id => court_user_id)
    end

    Task.transaction do
    @task.task_users = task_users
    @saved = @task.save
    end

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs