# Init lifecycle and rails

**URL:** <https://www.ruby-forum.com/t/init-lifecycle-and-rails/61915>\
**Category:** Rails\
**Created:** [May 29, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/init-lifecycle-and-rails/61915 "2006-05-29T18:56:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [May 29, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/init-lifecycle-and-rails/61915/1 "2006-05-29T18:56:29Z")

</div>

In a servlet J2EE application, there is an init style method that gets  
called after that servlet is loaded. Is there something similar within  
a rails controller context. I am looking for something where I can load  
and setup a particular object.

Also, what about with sessions as well. I would like to call some  
library when a user session begins and possibly kill that object when  
the session ends.

–  
Berlin B.  
(ramaza3 on freenode)  
[http://www.newspiritcompany.com](http://www.newspiritcompany.com)  
[http://www.newspiritcompany.com/newforums](http://www.newspiritcompany.com/newforums)  
also checkout alpha version of botverse:  
[http://www.newspiritcompany.com:8086/universe\_home](http://www.newspiritcompany.com:8086/universe_home)

---

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [May 29, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/init-lifecycle-and-rails/61915/2 "2006-05-29T19:14:38Z")

</div>

On 5/29/06, Berlin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In a servlet J2EE application, there is an init style method that gets  
> called after that servlet is loaded. Is there something similar within  
> a rails controller context. I am looking for something where I can load  
> and setup a particular object.

Perhaps you want a before\_filter?

[http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html#M000150](http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html#M000150)

> Also, what about with sessions as well. I would like to call some  
> library when a user session begins and possibly kill that object when  
> the session ends.

Sessions are usually automatically created on Rails, by default, upon  
first access. So it seems doubtful that there is a special callback,  
since they are “nothing special”. However, you can easily setup a  
global method (through ApplicationController) that would  
setup/teardown session, something like start\_session/end\_session.
