Sometimes it is helpful to have default values for an attribute. You have two options when it comes to this
Option 1: Default value contraint in database:
class Post < ActiveRecord::Base
self.description = "My Default Value" if self.new_record?
I added the if self.new_record? portion because after_initialize runs when building a new record or after fetching a record. I only want the value to be set on newly built records.
I think that Option 2 is more flexible but Option 1 has its perks too
Related External Links: