I am attempting to add a column to an existing table using an Active Record migration in Rails. I need the column's initial value to be based on other columns in the table. Is there a way to do this in an Active Record migration?
To make it more concrete, let's say I have the following:
my_table
----------------
first_name: text
last_name: text
I want to add a full_name
text column with an initial value of concat(first_name, ' ', last_name'
. Note that I don't want a default value on the column, as I intend for the application to be populating this going forward (the initial default is just to have a sensible starting value for existing records).
How can I do this in a migration? Ideally I would like to use add_column
or similar, though if that can't work a working alternative would be acceptable.
Note that there already exists a nearly identical question (add a database column with Rails migration and populate it based on another column), but none of its answers seem to fully answer this question.