I am trying to set some default values for an object using after_initialize. The problem I am having is that I would like this to be called no matter how the object is created.
My class:
class Foo < ActiveRecord::Base
serialize :data
after_initialize :init
def init
self.data ||= {}
self.bar ||= "bar"
self.baz ||= "baz"
end
end
Everything works fine if I call Foo.new, Foo.new(:bar => "things") and Foo.create(:baz => 'stuff'). However when I use a block with create the after_initialize callback doesn't get run.
obj = Foo.create do |f|
f.bar = "words"
f.data = { :attr_1 => 1, :attr_2 => 2 }
end
This just yields obj.baz => nil instead of "baz" with the other attributes set correctly.
Am I missing something with the way callbacks are executed, with the differences with calling create with a block and without or are default values getting clobbered by the block?
UPDATE
Found the issue.
It turns out that calling create with block and without are subtly different. When you call create without a block and just pass in a hash of parameters, for all intents and purposes you are calling Foo.new({<hash of argument>}).save, and the after_initialize callback gets executed right before the save like you would expect.
When you call create with a block something a little different happens. The order of events is Foo.new is called with whatever arguments you pass in, then after_initialize gets called, then the block gets run. So if you are using the block (as I was) interchangeably with the hash parameters just to make things a little bit more readable you could get bit because your after_initialize is run before all of the parameters you intend to set are actually set.
I got bit because I was doing some extra work in the after_initialize setting some extra required attributes based on the value of what was getting passed. Since nothing was actually set when after_initialize got called, nothing got set correctly and my validations failed.
I ended up having to make to calls to init. Once on after_initialize and once on before_validation. Not the cleanest, but it solved the issue.
Thanks go to Brandon for pointing me in the right direction.
Foo.new(i.e. with the block method of declaration), does the same thing happen?