# Updating instance variable

**URL:** <https://www.ruby-forum.com/t/updating-instance-variable/171085>\
**Category:** Rails\
**Created:** [June 26, 2009, 7:11am UTC](https://www.ruby-forum.com/t/updating-instance-variable/171085 "2009-06-26T07:11:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [June 26, 2009, 7:11am UTC](https://www.ruby-forum.com/t/updating-instance-variable/171085/1 "2009-06-26T07:11:41Z")

</div>

I am currently working on an app in which I a list of tasks displayed  
under a particular project. These tasks get displayed when the  
application first loads. I am setting this project as the current  
project. I have a method in my application controller which sets the  
last project created by the user as the current project.

//Code  
def current\_project  
@current\_project = Project.find(:last, :conditions =\> {:owner  
=\>  
current\_user.id})  
end

Now the user has other projects as well and he can open those  
projects  
as well. I can load those projects and get the tasks out of them  
using  
Project.find(params[:id]).tasks.

But the problem is that I am trying to set this project to the  
@current\_project.

My tasks controller looks like this:  
//Code  
def index  
debugger  
@tasks = @current\_project.tasks  
@tasks = @tasks.sort\_by{|i| i[:position]}  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @tasks }  
end  
//Code  
def load\_tasks  
debugger  
@current\_project = Project.find(params[:id])  
redirect\_to tasks\_url  
end

The load\_tasks action is being called on clicking of the project link  
so as to set the new value to @current\_project and then redirect to  
the index action. But this doesn’t seem to work and the value of the  
@current\_project stays the same in index action (which the value  
which  
was there when the application was loaded).

Is there a way that I can set the value to the @current\_project? I  
also tried to implement a getter and setter method but I don’t think  
I  
implemented it correctly or even if would help my cause here.  
If someone can point me in the right direction that will be great.  
Also I am a newbie to rails, i would like to apologize in advance in  
case this is a real fundamental mistake.

Regards  
vishy

---

<div class="post-metadata">

**Author:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [June 26, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/updating-instance-variable/171085/2 "2009-06-26T18:16:27Z")

</div>

Instance variables are lost at the end of the current request, so  
setting them and redirecting won’t accomplish anything. You might want  
to consider stashing the current project’s ID in the session, where it  
will be accessible to other actions.

–Matt J.
