I have a table which need new column. The newly introduced column need some value, which can be calculated from other fields in the row. Default does not help.
So, I created two migrations (possibly one) which will:
- Add a column (say: new_column)
- Update the column using attr_accessor
If I ran these migrations one by one, it run without problem.
But when I ran both migrations with a single migration request to rake, it looks like the attr_accessor is not synthesized after migration 1, so step 2 fails.
Currently, I call rake several times (each time, stop at after migration like step 1 above)
to avoid this.
I think I can synthesize attr_accessor before start using it, but don’t know what is the best practice in this situation.
What is the best way to solve this issue?
You should call
reset_column_informationon the class inside the migration which will reload the columns in that class.You would ideally do something like this:
Do not use an
attr_accessorever for any real column in your database. This overrides the default setter and getter methods for your column and will mean that attempting to set any value to this column won’t work. This is because you’re now using a virtual attribute, rather than a real one.