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

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

or to participate.