# Creating a temporary User

**URL:** <https://www.ruby-forum.com/t/creating-a-temporary-user/127647>\
**Category:** Rails\
**Created:** [January 31, 2008, 2:02am UTC](https://www.ruby-forum.com/t/creating-a-temporary-user/127647 "2008-01-31T02:02:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [January 31, 2008, 2:02am UTC](https://www.ruby-forum.com/t/creating-a-temporary-user/127647/1 "2008-01-31T02:02:26Z")

</div>

I am confused as to how to create a temporary User to retrieve / set  
tasks to. I am fairly certain this is the direction I am supposed to  
be moving in, but can’t figure out what I am doing wrong. Can anyone  
point me in the right direction? I want to do this “the right  
way”…rails 2.0, rest, etc.

# Tasks controller

def index  
@user = find\_user  
@tasks = Task.find(:all, :conditions =\> [“user\_id = ?, completed  
is null”, @user])  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @tasks }  
end  
end  
…  
def find\_user  
# if there isn’t a user return a new one with a fake name  
unless session[:user]  
session[:user] = User.new  
end  
session[:user]  
logger.info “#{session[:user]}”  
end

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [January 31, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/creating-a-temporary-user/127647/2 "2008-01-31T19:30:09Z")

</div>

Assuming that you have pushed a real User object into session at some  
point then, yes, that should work.

The one change that you should probably make work be to use the  
association proxies to do some of the work for you.

Assuming that  
User has\_many :tasks  
Task belongs\_to :user

Then the second line of your index method should read:

@tasks = @user.tasks.find(:all, :conditions=\>{:completed =\> nil})

The advantage here is that you can choose to reorganize the  
relationships between the user and tasks (e.g., assign one task to  
multiple users through a join table when someone is sick or  
unavailable) but your TasksController code does not have to change.  
The current code is tightly coupled to the current object  
relationship.
