Skip to content

Instantly share code, notes, and snippets.

@ddellacosta
Created August 20, 2011 07:56
Show Gist options
  • Save ddellacosta/1158825 to your computer and use it in GitHub Desktop.
Save ddellacosta/1158825 to your computer and use it in GitHub Desktop.
Initializer not getting called?
module Thinger
extend ActiveSupport::Concern
module ClassMethods
def acts_as_something
class_eval do
has_many :assignments
has_many :things, :through => :assignments
attr_accessible :things
after_initialize :set_default_thing
validate :must_have_at_least_one_thing
end
end
end
# This method is not getting called, apparently?
def set_default_things
if (things.size == 0)
things << Thing.find_or_create_new(:things_name)
end
end
end
# This test fails:
it "should set the 'things_name' thing on initialization if things are empty" do
no_thing_doohickey = Factory(:doohickey)
no_thing_doohickey.has_thing?(:things_name).should be_true
end
# But it works in console just fine:
irb(main):002:0> @d = Doohickey.new
...
irb(main):003:0> @d.things
=> [#<Thing id: 1, name: "things_name", description: nil>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment