[ACCEPTED]-How do I use functions like CONCAT(), etc. in ARel?-arel

Accepted answer
Score: 18

Use NamedFunction:

name = Arel::Attribute.new(Arel::Table.new(:countries), :name)
func = Arel::Nodes::NamedFunction.new 'zomg', [name]
Country.select([name, func]).to_sql


Score: 0

You can also use the Arel Extensions gem to have simpler 1 access to functions.

> User.where((User[:login] + User[:first_name]).length.in 2..10).to_sql
"SELECT `users`.* FROM `users` 
 WHERE LENGTH(CONCAT(CAST(`users`.`login` AS char),
                     CAST(`users`.`first_name` AS char)))
         BETWEEN (2) AND (10)"

More Related questions