# Only one "initialize" method allowed on AR objects?

**URL:** <https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151>\
**Category:** Rails\
**Created:** [January 4, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151 "2007-01-04T22:42:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 4, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/1 "2007-01-04T22:42:55Z")

</div>

I’m having some trouble with some seemingly basic OO stuff with one of  
my AR::Base descendants.

I ended up creating a custom initialize method that takes two parameters

- an integer and the standard AR attributes hash, like so:

In another part of my application, I was calling “clone” on one of these  
objects and it gave me an incorrect number of arguments error. I  
figured out it was because of the fact that my initialize expects 1  
argument no matter what.

As soon as I provided a default value for user\_id in the argument list  
above, the call to clone was fine - I assume since now my initialization  
didn’t require any arguments at all.

But this is somewhat disturbing. I was under the impression that I  
would still have access to the regular initialize(attributes = nil)  
method that I get from ActiveRecord::Base. Why is it not available?

Doesn’t this imply that I can’t have multiple constructors for an AR  
object?

I tried to add

def initialize(attributes = nil)  
super  
end

to my object and I still can’t instantiate it just running “new” (no  
arguments).

What gives?

Thanks for any help.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 4, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/2 "2007-01-04T23:14:51Z")

</div>

Use the :after\_initialize callback (or one of the other AR callbacks)  
rather than trying to override the initialize method. This is the  
recommended Rails approach.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 5, 2007, 1:06am UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/3 "2007-01-05T01:06:12Z")

</div>

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.” 🙂 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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 5, 2007, 7:43am UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/4 "2007-01-05T07:43:18Z")

</div>

Alex W. wrote:

> You can also use super
> 
> def initialize(foo, \*args)  
> @foo = foo  
> super(\*args)  
> end

This is what I was doing. If you do this and then attempt to call clone  
on your AR::Base descendant, it will complain.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 5, 2007, 7:28am UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/5 "2007-01-05T07:28:27Z")

</div>

Mick S. wrote:

> Use the :after\_initialize callback (or one of the other AR callbacks)  
> rather than trying to override the initialize method. This is the  
> recommended Rails approach.

You can also use super

def initialize(foo, \*args)  
@foo = foo  
super(\*args)  
end

The super method calls the same method as defined in the parent class.  
This let’s you override a method, and still call the original method.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 15, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/6 "2007-01-15T20:45:05Z")

</div>

I now know that there is no method overloading within the context of one  
class due to the dynamic nature of Ruby. So I get it.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/only-one-initialize-method-allowed-on-ar-objects/81151/7 "2007-01-06T19:30:21Z")

</div>

Just a quick glance as I’m out the door, but don’t you mean this to be a  
class method IE self.initialize?  
Jason
