Rails: How do I create a default value for attributes in Rails activerecord’s model? [duplicate]

You can set a default option for the column in the migration

....
add_column :status, :string, :default => "P"
....

OR

You can use a callback, before_save

class Task < ActiveRecord::Base
  before_save :default_values
  def default_values
    self.status ||="P" # note self.status="P" if self.status.nil? might better for boolean fields (per @frontendbeauty)
  end
end

Leave a Comment