title: Add position column to a model and set value on it based on association using single query
body: <p>I'm trying to add column position to a model and need to set value on it , i know i can do it like this:</p>
  <pre><code>Modell.where(id: 213835).associations.each_with_index do |association,index| 
    association.position = index + 1
    association.save!
  end
  </code></pre>
  <p>this will execute multiple query , i'm looking to achieve it on single query like explained <a href="https://stackoverflow.com/questions/54873957/updating-a-table-to-increment-a-value-per-user-id">here</a></p>
  <pre><code>SET @n = 0, @p = 0;
  UPDATE associations
  SET position = (@p := IF(model_id=@n, @p+1, 0)),
      model_id = (@n := model_id)
  WHERE model_id = 213835
  ORDER BY model_id, id;
  </code></pre>
  <p>the above works on MySQLWorkbench but i'm getting error as follows on rails app</p>
  <pre><code>    query = &lt;&lt;-SQL
          SET @n = 0, @p = 0;
          UPDATE associations
          SET position = (@p := IF(model_id=@n, @p+1, 0)),
            model_id = (@n := model_id)
          WHERE model_id = 213835
          ORDER BY model_id, id;
      SQL
  
  result = ActiveRecord::Base.connection.execute(query)
  
  ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE associations)
  SET position = (@p := IF(model_id=@n, @p, 0)),
  model_id = ' at line 2
  </code></pre>
  <p>any help is highly appreciated</p>