Creating a temporary User

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs