Short Ruby Newsletter

Share this post

How to safely remove a column in Rails

newsletter.shortruby.com
Code Summaries

How to safely remove a column in Rails

#code #summary #codesummary #delete #column #ruby #rails

Lucian Ghinda
Oct 12, 2022
4
Share this post

How to safely remove a column in Rails

newsletter.shortruby.com
# 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

Share this post

How to safely remove a column in Rails

newsletter.shortruby.com
Previous
Next
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 Lucian Ghinda
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing