Call Montréal, Canada 524-949-6869

Blog

Wednesday, September 15, 2010

Custom default attribute in ActiveRecord

When I need a default value for a ActiveRecord attribute often I will resort to using a plugin. But in reality, initializing an attribute in the #after_initialize callback is often the simplest thing that works:

class Tweet
  def after_initialize
    self.message ||= "I'm Tweeting"
  end
end         

Developing Ruby on Rails applications and custom Content Management Systems in Montréal. Contact Firsthand.