# Instance variable life span in controller

**URL:** <https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180>\
**Category:** Rails\
**Created:** [July 16, 2009, 12:02pm UTC](https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180 "2009-07-16T12:02:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [July 16, 2009, 12:02pm UTC](https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180/1 "2009-07-16T12:02:19Z")

</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.  
#######################################  
Class AppSetting\< ActionRecrod::Base  
end

Class PeopleController \< ApplicationController  
before\_filter :get\_title  
before\_filter :get\_subtitle  
after\_filter :update\_some\_properties\_on\_condition  
after\_filter :update\_some\_other\_properties\_on\_condition

```
def index
   @name= Person.first.name
end

private

def get_title
   @title = AppSetting.first.title
end

def get_subtitle
   @subtitle = AppSetting.first.subtitle
end

def update_some_properties_on_condition
   do_something if condition1 == AppSetting.first.condition1
end

def update_some_other_properties_on_condition
   do_something if condition2 == AppSetting.first.condition2
end

```

end  
#######################################################

As you can see, all the five methods all using “AppSetting.first”  
object in the process.

Is there anyway that I can set some variables that all actions can use  
in this process? just like the function of class variables in ordinary  
ruby code.

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [July 16, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180/2 "2009-07-16T12:54:06Z")

</div>

You can do like

## def get\_title @title = @first\_setting.title end

def first\_app\_setting  
@first\_setting ||= AppSetting.first  
end

Sijo

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [July 16, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180/3 "2009-07-16T12:08:46Z")

</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:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [July 16, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/instance-variable-life-span-in-controller/172180/4 "2009-07-16T12:59:43Z")

</div>

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

Sijo
