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.
On Jan 30, 8:01 pm, yaphi
<jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---