I''m new to rails and have a question that seems simple but that I cannot resolve. Is there a way to find the id of the currently logged in user? Thanks, JP -- Posted via http://www.ruby-forum.com/.
JP, It depends on what authorization mechanism you are using, but if the complete user info is stored in the session it would be something like... session[:user].id or, if you are just storing the id in the session and doing a lookup for each access to user data... session[:user] substitute your session variable for the "user" entry. Hope that helps. Nathan -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Jeremy Pettet Sent: Thursday, May 11, 2006 3:22 PM To: rails@lists.rubyonrails.org Subject: [Rails] current user''s id? I''m new to rails and have a question that seems simple but that I cannot resolve. Is there a way to find the id of the currently logged in user? Thanks, JP -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
If you mean the OS User-Id, there isn''t one. You could use cookies and sessions and assign an id. Or do you mean a database id unique key? Warren Seltzer -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Nathan Leach Sent: Thursday, May 11, 2006 11:44 PM To: rails@lists.rubyonrails.org Subject: RE: [Rails] current user''s id? JP, It depends on what authorization mechanism you are using, but if the complete user info is stored in the session it would be something like... session[:user].id or, if you are just storing the id in the session and doing a lookup for each access to user data... session[:user] substitute your session variable for the "user" entry. Hope that helps. Nathan -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Jeremy Pettet Sent: Thursday, May 11, 2006 3:22 PM To: rails@lists.rubyonrails.org Subject: [Rails] current user''s id? I''m new to rails and have a question that seems simple but that I cannot resolve. Is there a way to find the id of the currently logged in user? Thanks, JP -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Warren Seltzer wrote:> If you mean the OS User-Id, there isn''t one. You could use cookies and > sessions and > assign an id. Or do you mean a database id unique key? > > Warren SeltzerI meant the id in the ''users'' table. This works for me: @session[''user''].id Out of curiosity I looked at the complete ''user'' hash: @session[''user''] And it returned the groups that the user is a member of. And I cannot find where this information is placed in the session. Is it normal for the user to include the group members from a many-to-many relation (ie. users <-- groups_users --> groups)? Thanks JP -- Posted via http://www.ruby-forum.com/.
You should be careful about storing the user object in the session. It''s associations will be cached from the first time they are accessed. Either just store the id and query the user each request, or be very sure to reload the stored object and associations when necessary. I prefer the approach of storing the user id in the session and querying it each request. class User < ActiveRecord::Base cattr_accessor :current end Then populate User.current with each request: class ApplicationController < ActionController::Base before_filter do User.current = User.find(session[:user_id]) end end Then you can use User.current anywhere to get the currently logged in user. -Jonathan. On 5/12/06, Jeremy Pettet <jeremy.pettet@gmail.com> wrote:> Warren Seltzer wrote: > > If you mean the OS User-Id, there isn''t one. You could use cookies and > > sessions and > > assign an id. Or do you mean a database id unique key? > > > > Warren Seltzer > > I meant the id in the ''users'' table. > This works for me: @session[''user''].id > > Out of curiosity I looked at the complete ''user'' hash: > > @session[''user''] > > And it returned the groups that the user is a member of. And I cannot > find where this information is placed in the session. > > Is it normal for the user to include the group members from a > many-to-many relation (ie. users <-- groups_users --> groups)? > > Thanks > JP > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jeremy Pettet wrote:> I''m new to rails and have a question that seems simple but that I cannot > resolve. > > Is there a way to find the id of the currently logged in user? > > Thanks, > JPMaybe you can store it in the session when they log in. Then it''s there and you can just pull it out whenever you need it. I''m new too. sean -- Posted via http://www.ruby-forum.com/.
sean colquhoun wrote:> Jeremy Pettet wrote: >> I''m new to rails and have a question that seems simple but that I cannot >> resolve. >> >> Is there a way to find the id of the currently logged in user?not sure I understand with ''logged in user'', if you have authentication enabled, then you get the User object from the username stored in the session, but if you just want to access the current session id then perhaps this is what you need: session.session_id or look in the rails API for Module ActionController::SessionManagement regards Isabelle -- Posted via http://www.ruby-forum.com/.
sean colquhoun wrote:> Jeremy Pettet wrote: >> I''m new to rails and have a question that seems simple but that I cannot >> resolve. >> >> Is there a way to find the id of the currently logged in user?not sure I understand what is meant here with ''logged in user''... if you have authentication enabled, then you get the User object from the username stored in the session, but if you just want to access the current session id then perhaps this is what you need: session.session_id maybe have a look in the rails API for Module ActionController::SessionManagement regards Isabelle -- Posted via http://www.ruby-forum.com/.