Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It hasn't received much usage yet but I wrote the following code which seems to solve the problem. This monkey-patches the adapters to have a method to support it:</p> <pre><code>module ActiveRecord module ConnectionAdapters class AbstractAdapter # Will return the given strings as a SQL concationation. By default # uses the SQL-92 syntax: # # concat('foo', 'bar') -&gt; "foo || bar" def concat(*args) args * " || " end end class AbstractMysqlAdapter &lt; AbstractAdapter # Will return the given strings as a SQL concationation. # Uses MySQL format: # # concat('foo', 'bar') -&gt; "CONCAT(foo, bar)" def concat(*args) "CONCAT(#{args * ', '})" end end class SQLServerAdapter &lt; AbstractAdapter # Will return the given strings as a SQL concationation. # Uses MS-SQL format: # # concat('foo', 'bar') -&gt; foo + bar def concat(*args) args * ' + ' end end end end </code></pre> <p>With this you should be able to do the following in your code:</p> <pre><code>class User &lt; ActiveRecord::Base def self.find_by_name(name) where("#{connection.concat('first_name', 'last_name')} = ?", name) end end </code></pre> <p>This outputs the following SQL query on a SQL-92 database (Oracle, SQLite, PostgreSQL):</p> <pre><code>SELECT * FROM users WHERE first_name || last_name = ? </code></pre> <p>For MySQL it outputs:</p> <pre><code>SELECT * FROM users WHERE CONCAT(first_name, last_name) = ? </code></pre> <p>For SQL Server it outputs</p> <pre><code>SELECT * FROM users WHERE first_name + last_name = ? </code></pre> <p>Obviously you could extend this concept to other database adapters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload