I'm so used to oracle where you can simply
concat(field1, ' ', field2)
but if I'm using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this?
Cheers for all your help
in your model:
def full_name
[first_name, last_name].join(' ')
end
For posterity and future googlers, you can do the following assuming postgres(maybe mysql?):
User.select("(first_name || ' ' || last_name) as name").where(organization: current_user.organization)
The select uses the ||
SQL operator to concat the strings from the fields first_name
and last_name
, and as name
returns the result in a column "name".
Which might return:
+----+--------------------+
| id | name |
+----+--------------------+
| 3 | Ada Lovelace |
| 18 | Alan Turing |
+----+--------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With