# Accessing the instance variable in another method

**URL:** <https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098>\
**Category:** Rails\
**Created:** [September 22, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098 "2009-09-22T14:51:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 22, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/1 "2009-09-22T14:51:34Z")

</div>

Hi,  
I am new to Rails.I tried developing a basic quiz application.Please  
help me out in my doubt.Thanks in advance

\<% CODE %\>

class QuizController \< ApplicationController

def index

# i have retrieved the first five questions from the database for

displaying the #question to user  
@quiz=Quiz.find(:all,:limit=\>5)

end

def report

end

# this method checks the answer and gives score

def checkanswer

# here i want to access the @quiz object which is a set of questions i

have #retreived from the database.Hot to access it?  
end

end

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 22, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/2 "2009-09-22T17:20:14Z")

</div>

Subashini K. wrote:

> Hi,  
> I am new to Rails.I tried developing a basic quiz application.Please  
> help me out in my doubt.Thanks in advance
> 
> \<% CODE %\>
> 
> class QuizController \< ApplicationController
> 
> def index
> 
> # i have retrieved the first five questions from the database for
> 
> displaying the #question to user  
> @quiz=Quiz.find(:all,:limit=\>5)

You should really call this variable @quizzes, so it will be clear that  
it contains an Array of Quiz objects, not just one.

> end
> 
> def report
> 
> end
> 
> # this method checks the answer and gives score
> 
> def checkanswer
> 
> # here i want to access the @quiz object which is a set of questions i
> 
> have #retreived from the database.Hot to access it?

You’ll need to retrieve the records again, or pass them in from params  
or the session. Instance variables do not persist between controller  
method calls (because the controller is freshly instantiated each time a  
request is made).

> end
> 
> end

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 22, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/3 "2009-09-22T17:21:12Z")

</div>

Subashini K. wrote:

> Hi,  
> I am new to Rails.I tried developing a basic quiz application.Please  
> help me out in my doubt.Thanks in advance
> 
> \<% CODE %\>
> 
> class QuizController \< ApplicationController
> 
> def index
> 
> # i have retrieved the first five questions from the database for
> 
> displaying the #question to user  
> @quiz=Quiz.find(:all,:limit=\>5)
> 
> end
> 
> def report
> 
> end
> 
> # this method checks the answer and gives score
> 
> def checkanswer
> 
> # here i want to access the @quiz object which is a set of questions i
> 
> have #retreived from the database.Hot to access it?  
> end

> end

hi Subashini,

I think you can use @quiz variable by making it a class variable as  
@@quiz or a global variable $quiz.

might help,

Saurabh

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 23, 2009, 12:56am UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/4 "2009-09-23T00:56:13Z")

</div>

On Tue, Sep 22, 2009 at 8:51 PM, Saurabh P. \<  
removed\_email\_address@domain.invalid\> wrote:

> hi Subashini,
> 
> I think you can use @quiz variable by making it a class variable as  
> @@quiz or a global variable $quiz.
> 
> Certainly, a wrong way to do it. Don’t do it (at least in rails controller)

As suggested earlier, you should pass ids in params or store in session  
to  
retrieve in your check\_answer method.

> might help,
> 
> ## Saurabh
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 23, 2009, 3:56am UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/5 "2009-09-23T03:56:41Z")

</div>

Hi –

See my answer on ruby-talk.

David

–  
David A. Black, Director  
Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
Ruby/Rails training, consulting, mentoring, code review  
Book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [September 22, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/accessing-the-instance-variable-in-another-method/197098/6 "2009-09-22T18:07:57Z")

</div>

Saurabh P. wrote:  
[…]

> hi Subashini,
> 
> I think you can use @quiz variable by making it a class variable as  
> @@quiz or a global variable $quiz.

DON’T DO THAT! That will cause other problems.

> might help,
> 
> Saurabh

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
