How to set default values in Rails?

后端 未结 17 1385
醉话见心
醉话见心 2020-11-28 04:39

I\'m trying to find the best way to set default values for objects in Rails.

The best I can think of is to set the default value in the new method in

17条回答
  •  鱼传尺愫
    2020-11-28 05:04

    You can also try change_column_default in your migrations (tested in Rails 3.2.8):

    class SetDefault < ActiveRecord::Migration
      def up
        # Set default value
        change_column_default :people, :last_name, "Smith"
      end
    
      def down
        # Remove default
        change_column_default :people, :last_name, nil
      end
    end
    

    change_column_default Rails API docs

提交回复
热议问题