How to set default values in Rails?

后端 未结 17 1387
醉话见心
醉话见心 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

    In case you're dealing with a Model, you can use the Attriutes API in Rails 5+ http://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute

    just add a migration with a proper column name and then in the model set it with:

    class StoreListing < ActiveRecord::Base
      attribute :country, :string, default: 'PT'
    end
    

提交回复
热议问题