title: How can I sort ActiveRecord Objects by id array
body: <p>My Env.</p>
  
  <pre><code>ruby 2.0.0-p195
  rails (4.0.0.rc1)
  activerecord (4.0.0.rc1)
  </code></pre>
  
  <p>I want to sort ActiveRecord Objects by id array.
  I tried with order by field.</p>
  
  <pre><code>ids = [1,4,2,3]
  Foo.where(id: ids).order('FIELD(id, ?)', ids)
  </code></pre>
  
  <p>However it's failed.</p>
  
  <pre><code>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 '?,?,?,?}), ö, ÷, ï, ñ' at line 1:
  </code></pre>
  
  <p>Then I try </p>
  
  <pre><code>ids = [1,4,2,3]
  Foo.where(id: ids).order('FIELD(id, #{ids.join(","))')
  </code></pre>
  
  <p>it's of course success. However I'm afraid that it may have SQL Injection risk because Array ids are generated from session value.</p>
  
  <p>Is there any better and secure ways?</p>
  
  <p>Thanks in advance.</p>