# Controller level variables

**URL:** <https://www.ruby-forum.com/t/controller-level-variables/149172>\
**Category:** Rails\
**Created:** [September 22, 2008, 8:52am UTC](https://www.ruby-forum.com/t/controller-level-variables/149172 "2008-09-22T08:52:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaikishan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jaikishan](https://www.ruby-forum.com/u/Jaikishan)\
**Post date:** [September 22, 2008, 8:52am UTC](https://www.ruby-forum.com/t/controller-level-variables/149172/1 "2008-09-22T08:52:05Z")

</div>

Hello,

I am new to ruby and I want to know how can I define a variable whose  
scope is restricted to a particular controller.

Scenario:  
What I am trying to achieve is this - based on the parameters  
specified by user, I make some queries to database. The results of the  
query are consumed by different methods in that controller. What I  
want to achieve is - I want to make all the queries before the page  
loads and store them in some variable and I want to access these  
variables across different methods in the controller.

Thanks,  
Jaikishan

---

<div class="post-metadata">

**Author:** ![Jaikishan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jaikishan](https://www.ruby-forum.com/u/Jaikishan)\
**Post date:** [September 22, 2008, 9:06am UTC](https://www.ruby-forum.com/t/controller-level-variables/149172/2 "2008-09-22T09:06:43Z")

</div>

On Sep 22, 7:33 am, Jaikishan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> loads and store them in some variable and I want to access these  
> variables across different methods in the controller.

Sounds like you want an instance variable (it also sounds like you are  
spending too much time worrying about details without knowing about  
the basics)

Fred

---

<div class="post-metadata">

**Author:** ![Jaikishan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jaikishan](https://www.ruby-forum.com/u/Jaikishan)\
**Post date:** [September 22, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/controller-level-variables/149172/3 "2008-09-22T15:17:00Z")

</div>

> and I want to access these  
> variables across different methods in the controller.

You might also want to google for “skinny controller fat model” before  
you get too deep into your design…

as Fred mentioned, get a basic app up and running (scaffolding works  
fine for this, and will write the example code for you), then extend  
from there. Rails has a mindset you need to understand in order to build  
effectively.

I recently spoke with a friend who heard about Rails and tried it out,  
then complained when it was too difficult. Turned out he was trying to  
apply the same organization to his Rails code as he used on other  
projects not built in Rails. An hour or so refactoring his code to “the  
Rails way”, and he was astonished at the ease of use.
