- Short Ruby Newsletter
- Posts
- How to safely remove a column in Rails
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://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/bf7a505f-4d47-4285-8975-131ff6291f49/7f9a48f3-b165-49d2-b91c-b3fab74aca73_1388x826.png?t=1726892706)
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
Reply