jeudi 8 octobre 2015

Inerting multiple commonands into prepared statement rails

I am using rails 3 and I need to execute raw sql in one of my migration and I need to do it using prepared statement since it is the best way to escape problems which arise due to single, statement or so. Is there a way where in I can execute multiple sql statements in a single prepare statement. I am using PostgreSQL for my database

Here is my code what i tried

  CONN = ActiveRecord::Base.connection.raw_connection  
  sql = %Q[
            INSERT INTO table1 
              (
                name,
                email,
                phone,
                created_at,
                updated_at
               ) 
              VALUES 
              (
                $1, 
                $2, 
                current_timestamp, 
                current_timestamp
              );
            UPDATE table2 
              SET column_1 = $1
              WHERE id = $4;
            UPDATE contacts SET 
              column_2 = $2
              WHERE id = $4
          ]

  CONN.prepare('insert_and_update', sql)
  CONN.exe_prepared('insert_and_update', [
      name,
      email,
      phone,
      customer.id
    ])

But I am getting error as

 cannot insert multiple commands into a prepared statement

Aucun commentaire:

Enregistrer un commentaire