# Initialize called more than once?

**URL:** <https://www.ruby-forum.com/t/initialize-called-more-than-once/50410>\
**Category:** Rails\
**Created:** [December 16, 2005, 12:50am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410 "2005-12-16T00:50:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 16, 2005, 12:50am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/1 "2005-12-16T00:50:08Z")

</div>

Hi,

In my test case below, it seems that ‘initialize’ is called everytime  
‘hello’ is called. Is this by design, a bug, or my pilot error?

```
class FooController < ApplicationController

    def initialize
        print "\n initialize \n"
    end

    def hello
        print "\n hello \n"
    end
end

```

I thought initialize will be called exactly once when the controller is  
constructed and not when an action is executed.

Thanks for your help and sorry if duplicate,

-Danny

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 16, 2005, 12:53am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/2 "2005-12-16T00:53:09Z")

</div>

On 12/15/05, Danny H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def hello
> print "\n hello \n"
> end
> end
> 
> ```
> 
> I thought initialize will be called exactly once when the controller is  
> constructed and not when an action is executed.

On every request, a controller object is created. When the controller  
object is created, the initialized function is called.

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 16, 2005, 12:53am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/3 "2005-12-16T00:53:10Z")

</div>

Well the controler gets created every time a action is called, HTTP is a  
stateless protocol : )

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 16, 2005, 1:23am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/4 "2005-12-16T01:23:18Z")

</div>

If you’re in dev mode, I believe the controller would be reloaded  
with every request so you would see initialize called for each  
invocation of hello.

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 16, 2005, 12:53am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/5 "2005-12-16T00:53:11Z")

</div>

On Thu, 2005-12-15 at 15:47 -0800, Danny H. wrote:

> ```
> def hello
> print "\n hello \n"
> end
> end
> 
> ```
> 
> I thought initialize will be called exactly once when the controller is  
> constructed and not when an action is executed.

This is correct, it’s just that a new FooController is being created  
(instantiated, whatever) for each request.

- Jamie

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [December 18, 2005, 4:53am UTC](https://www.ruby-forum.com/t/initialize-called-more-than-once/50410/6 "2005-12-18T04:53:28Z")

</div>

On 12/15/05, Steven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you’re in dev mode, I believe the controller would be reloaded  
> with every request so you would see initialize called for each  
> invocation of hello.

Controllers get re-constructed on every invocation of every action, no  
matter if you’re in development, test, or production mode.
