Using `CONCAT` w/ Arel in Rails 4 Using `CONCAT` w/ Arel in Rails 4 ruby ruby

Using `CONCAT` w/ Arel in Rails 4


With the latest Arel it's required to use Arel::Nodes.build_quoted(' ') instead of just String (' '). So the answer nowadays is:

SEPARATOR = Arel::Nodes.build_quoted(' ')Arel::Nodes::NamedFunction.new(  'concat',  [arel_table[:first_name], SEPARATOR, arel_table[:last_name]])


If you want an equal search

 where(concat.eq("john smith")) SELECT "users".* FROM "users" WHERE CONCAT("users"."first_name", "users"."last_name") = 'john smith'

If you want a like search

where(concat.matches("%john smith%")) SELECT "users".* FROM "users" WHERE (CONCAT("users"."first_name", "users"."last_name")) ILIKE '%admin%'

There are other methods given in the documentation that you can use

You might want a space in your concat

concat = Arel::Nodes::NamedFunction.new( 'concat',  [arel_table[:first_name],   ' ',   arel_table[:last_name]])