What is wrong with this initialize method (code example)?


#1

Here is my class:

class Document < ActiveRecord::Base

public
def initialize
puts “Called initialize…”
super.initialize
end

This generates the following error:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occured while evaluating nil.initialize

WTF?

Wes


#2

I think you just need to call super, not super.initialize


#3

From the “programming ruby” book…

Within the body of a method, a call to super acts just like a call to
that
original method, except that the search for a method starts in the
superclass of the object that was found to have the original method.

Explains why you are getting the nil.initialize error. ‘super’ is like
calling initialize, which returns nil.