Only one "initialize" method allowed on AR objects?

Thanks Mick.

I'm aware of the after_initialize method, but I just keep forgetting that ActiveRecord objects don't behave like normal objects.

"Least surprise" my a** :). Strictly speaking, I suppose, Ruby is "least surprise", Rails is "opinionated." :slight_smile: But don't get me wrong, I really dig ActiveRecord.

Still, it's weird to me that the existence of a method (initialize in this case) seem to preclude any polymorphism on that method.

Wes

Wes-

  You can define your own initialize methods you just have to make sure to call super properly. SInce you are changing the arity of the real initialize you need to call super accordingly.

Lets show a simple test

class A
   def initialize(a)
     @a = a
     puts @a
   end
end

class B < A
     def initialize(a=nil,b=nil)
       super(a)
       @b = b
       puts @a
       puts @b
     end
end

>> A.new 'one'
one
# => #<A:0x30cdfc @a="one">

>> B.new 'one', 'two'
one
two
# => #<B:0x307884 @a="one", @b="two">

  When you just call super without arguments then it calls the super initialize with the same args that were passed to your redefined initialize so thats probably why it fails. I'm not sure entirely what you need to accomplish but finding the right call to super will definitely let you define your own. But AR objects are tricky to deal with all the complexity so after_initialize is usually a better choice.

Cheers-
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)