# instance variable life span in controller

**URL:** <https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296>\
**Category:** rubyonrails-talk\
**Created:** [July 16, 2009, 10:01am UTC](https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296 "2009-07-16T10:01:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boblu](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@boblu](https://discuss.rubyonrails.org/u/boblu)\
**Post date:** [July 16, 2009, 10:01am UTC](https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296/1 "2009-07-16T10:01:31Z")

</div>

I understand that rails drops all instance variables created in normal controller action after rendering view templates.

But I have the following problem that a normal controller action has several before\_filter and after\_filter, which all using same set of variables fetching from database.

---

<div class="post-metadata">

**Author:** ![boblu](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@boblu](https://discuss.rubyonrails.org/u/boblu)\
**Post date:** [July 16, 2009, 10:07am UTC](https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296/2 "2009-07-16T10:07:40Z")

</div>

I do not think I explained my problem clearly.

After you read the above post, you maybe think that I can just create another before\_filter which is at the very beginning of this process, and just set an instance variable like @app\_setting = App.first, and all the following method can use this variable.

But because all the before\_filter and after\_filter are wrapped to almost all actions in my app, so what I really want is that whether there is something that can persists its value between different request?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 16, 2009, 10:54am UTC](https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296/3 "2009-07-16T10:54:06Z")

</div>

You can do like

def get\_title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@title = @first\_setting.title end

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 16, 2009, 10:59am UTC](https://discuss.rubyonrails.org/t/instance-variable-life-span-in-controller/44296/4 "2009-07-16T10:59:43Z")

</div>

Sorry def get\_title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@title = first\_app\_setting.title end

Sijo
