My table has a column “order” that I want initially set to equal the record’s ID.
Can I do this in the migration, or do I need to set it in the create action?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you already have data, you can’t do this in a create callback action because those items have already been created. However, a create callback would help you for new objects you will create:
You can technically update existing data in a migration as a SQL statement, but I wouldn’t recommend using commands involving the ActiveRecord model as the migration may break sometime down the line as the model changes. To do this in a migration you would have an execute command in the :up method (not sure what behavior you would want for :down) like so:
Alternatively you could create a separate rake task to do the update; this would keep your migrations cleaner and give you better control over when you will run the update task.