title: Rails MySQL ILIKE query
body: <p>My syntax is off.  I want to create a scope <code>by_name</code> that finds all <code>agencies</code> whose name attribute contains the passed in string (case insensitive). </p>
  
  <p>Here is what I have:</p>
  
  <pre><code>class Agency &lt; ActiveRecord::Base
    scope :by_name, -&gt;(agency_name) { where('name ILIKE ?', "%#{agency_name}%") }
  end
  </code></pre>
  
  <p>In the rails console I type in <code>agencies = Agency.by_name("foo")</code>.  Here is the query generated:</p>
  
  <pre><code>SELECT `agencies`.* FROM `agencies`  WHERE (name ILIKE '%foo%')
  </code></pre>
  
  <p>Here is the error message:</p>
  
  <blockquote>
    <p>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 'ILIKE '%foo%')</p>
  </blockquote>