I have a cars table which was created a year ago and needs to be renamed to vehicles table now.
Car table migration
class AddDataToCarsModel < ActiveRecord::Migration[5.0]
def change
# some cars are missing their created timestamp
Car.where(created_at: nil).each do |car|
date = Time.zone.now
car.update_attribute(:created_at, date)
end
end
end
Vehicle table renaming migration
class RenameCarsToVehicles < ActiveRecord::Migration[5.1]
def change
rename_table :cars, :vehicles
end
end
However, when dropping the current db and running the migration I get uninitialized constant AddDataToCarsModel::Car
error as I have already removed the cars model as a part of it.
What is a best practice for these situations? Is it worth running into old migration that was create a year ago and update it?
How can we handle these situations?
Thanks for the help.
As a fix of your migration you could change your AddDataToCarsModel
migration into this:
class AddDataToCarsModel < ActiveRecord::Migration[5.0]
class Car < ActiveRecord::Base
self.table_name = 'cars'
end
def change
# some cars are missing their created timestamp
Car.where(created_at: nil).each do |car|
date = Time.zone.now
car.update_attribute(:created_at, date)
end
end
end
So you can use Cars
model only in this migration. And this will not break anything into your app lifecycle.