Initialize Extra Attributes In Activerecord-classes
Join the DZone community and get the full member experience.Join For Free
Classes derived from ActiveRecord::Base don't call initialize on creation. Extra attributes (i.e. non DB-attributes) can be initialized with the after_initialize callback.
class Something < ActiveRecord::Base attr_reader :extra_attribute def after_initialize @extra_attribute = 'default_value' endReferences: - http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html - http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-initialize-values
Opinions expressed by DZone contributors are their own.