Add a default value to a column through a migration

后端 未结 7 1689
旧巷少年郎
旧巷少年郎 2020-11-28 02:16

How do I add a default value to a column that already exists through a migration?

All the documentation I can find shows you how to do it if the column doesn\'t alr

7条回答
  •  孤街浪徒
    2020-11-28 02:23

    This is what you can do:

    class Profile < ActiveRecord::Base
      before_save :set_default_val
    
      def set_default_val
        self.send_updates = 'val' unless self.send_updates
      end
    end
    

    EDIT: ...but apparently this is a Rookie mistake!

提交回复
热议问题