How to safely remove a column in Rails
#code #summary #codesummary #delete #column #ruby #rails
![# How to delete a column from a table # Step 1️⃣: Set the column to ignored class User < ActiveRecord::Base self.ignored_columns = [:username] end # Trying to access the column will raise an error u=User.first u.username # raises NoMethodError # Step 2️⃣: Change your code not to use any more that column # Step 3️⃣: Deploy to production # and monitor any NoMethodError errors reported # Step 4️⃣: Add a migration to drop the column # done](https://archive-assets.shortruby.com/assets/issues/5d8643fdcc0b8a1c3972913750d6eafd5df254391c2b037be72f879fc7cf477e.png)
Here is the text version:
# How to delete a column from a table
# Step 1️⃣: Set the column to ignored
class User < ActiveRecord::Base
self.ignored_columns = [:username]
end
# Trying to access the column will raise an error
u = User.first
u.username # raises NoMethodError
# Step 2️⃣: Change your code not to use any more that column
# Step 3️⃣: Deploy to production
# and monitor any NoMethodError errors reported
# Step 4️⃣: Add a migration to drop the column
# done