I need an extra field for custom sorting for my Profile model. So i need to initialize it on column addition.
Now I see two approaches to do that:
- Update field after migrations via rake task or at production console.
-
Write down something like that
Profile.all.each { |p| p.update_attribute :sorted_at, p.created_at }in separate migration file.
Is second solution good or bad? And may be is there 3rd solution which is much better than these?
My opinion is to update the fields in the migration.
Pros:
Cons:
Also, it’s a good idea to use
update_allinstead of looping through each record and updating because of the obvious performance increase.