# Listing values belonging to logged-in user

**URL:** <https://www.ruby-forum.com/t/listing-values-belonging-to-logged-in-user/112991>\
**Category:** Rails\
**Created:** [August 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/listing-values-belonging-to-logged-in-user/112991 "2007-08-25T16:45:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [August 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/listing-values-belonging-to-logged-in-user/112991/1 "2007-08-25T16:45:54Z")

</div>

I saw a post on creating user Management Using Salted Hash Login  
Generator. I followed the steps, but I cannot get it to work. I keep  
getting a nil

the troublesome code is in the plunge\_controller.rb:  
def list  
@plunge\_pages, @plunges = paginate :plunges, :conditions =\> [‘user\_id  
= ?’, @session[‘user’].id]  
end

This gives a “Called id for nil, which would mistakenly be 4 – if you  
really wanted the id of nil, use object\_id”

My plunge.rb model starts like this:  
class Plunge \< ActiveRecord::Base  
belongs\_to :user,  
:class\_name =\> “User”,  
:foreign\_key =\> “user\_id”

and my user,rb like this:  
class User \< ActiveRecord::Base  
has\_many :plunges,  
:class\_name =\> “User”,  
:foreign\_key =\> “user\_id”

…  
I can get it to work by hardcoding the user id like this:  
@plunge\_pages, @plunges = paginate :plunges, :conditions =\> ‘user\_id =  
1’

Anyone who can help?

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [August 25, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/listing-values-belonging-to-logged-in-user/112991/2 "2007-08-25T21:38:04Z")

</div>

Jan A. wrote:

> I saw a post on creating user Management Using Salted Hash Login  
> Generator. I followed the steps, but I cannot get it to work. I keep  
> getting a nil
> 
> the troublesome code is in the plunge\_controller.rb:  
> def list  
> @plunge\_pages, @plunges = paginate :plunges, :conditions =\> [‘user\_id  
> = ?’, @session[‘user’].id]  
> end
> 
> This gives a “Called id for nil, which would mistakenly be 4 – if you  
> really wanted the id of nil, use object\_id”
> 
> My plunge.rb model starts like this:  
> class Plunge \< ActiveRecord::Base  
> belongs\_to :user,  
> :class\_name =\> “User”,  
> :foreign\_key =\> “user\_id”
> 
> and my user,rb like this:  
> class User \< ActiveRecord::Base  
> has\_many :plunges,  
> :class\_name =\> “User”,  
> :foreign\_key =\> “user\_id”
> 
> …  
> I can get it to work by hardcoding the user id like this:  
> @plunge\_pages, @plunges = paginate :plunges, :conditions =\> ‘user\_id =  
> 1’
> 
> Anyone who can help?

@session[‘user’] is returning nil. (You knew this, right?) The question  
is why. Here are some things to check:

- Are sessions working? They usually require some configuration, esp.  
if they are stored in the database. If you have just dropped this  
library into a new Rails Application check this out first.

- Is the session being set properly? Check your controllers. If you  
cannot find a place where @session[‘user’] = you have a  
problem (Note: this may have come with the library code)

- Is the browser/test case your using preserving sessions between calls?  
I cannot tell you the number of times a stale browser cache in Fire Fox  
or a test case that is not session aware has blown my whole day.

As a final thought, a lot of these data persistence problems are very  
difficult to track down because there is no way to trace the execution  
path through the program. We MUST wait for the client to make a second  
request with the right data. To help me with this I have a debug  
template that I use for all my pages. The template contains a button at  
the bottom that will pop up a window which lists debug(@user),  
debug(params), debug(session), and debug(flash). Very helpful for these  
types of things.

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [August 26, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/listing-values-belonging-to-logged-in-user/112991/3 "2007-08-26T14:07:52Z")

</div>

Hi John,

Thanks for taking the time to help me. Your comments put me on the right  
track. I did some hours of debugging and decided to rewrite the thing  
as:  
def list  
@plunges = Plunge.find:all, :conditions =\> [“user\_id = ?”,  
session[:user\_id]]  
end

My login controller looks like this:  
def login  
session[:user\_id] = nil  
if request.post?  
user = User.authenticate(params[:name], params[:password])  
if user  
session[:user\_id] = user.id  
…

No matter what I do, I cannot get @session[‘user’].id to be anything  
other than nil, so I guess that session[:user\_id] is an ok replacement  
🙂

Anyway, Thanks again for your help.  
Jan

John M. wrote:

> Jan A. wrote:
> 
> > I saw a post on creating user Management Using Salted Hash Login  
> > Generator. I followed the steps, but I cannot get it to work. I keep  
> > getting a nil
> > 
> > the troublesome code is in the plunge\_controller.rb:  
> > def list  
> > @plunge\_pages, @plunges = paginate :plunges, :conditions =\> [‘user\_id  
> > = ?’, @session[‘user’].id]  
> > end
> > 
> > This gives a “Called id for nil, which would mistakenly be 4 – if you  
> > really wanted the id of nil, use object\_id”
> > 
> > My plunge.rb model starts like this:  
> > class Plunge \< ActiveRecord::Base  
> > belongs\_to :user,  
> > :class\_name =\> “User”,  
> > :foreign\_key =\> “user\_id”
> > 
> > and my user,rb like this:  
> > class User \< ActiveRecord::Base  
> > has\_many :plunges,  
> > :class\_name =\> “User”,  
> > :foreign\_key =\> “user\_id”
> > 
> > …  
> > I can get it to work by hardcoding the user id like this:  
> > @plunge\_pages, @plunges = paginate :plunges, :conditions =\> ‘user\_id =  
> > 1’
> > 
> > Anyone who can help?
> 
> @session[‘user’] is returning nil. (You knew this, right?) The question  
> is why. Here are some things to check:
> 
> - Are sessions working? They usually require some configuration, esp.  
> if they are stored in the database. If you have just dropped this  
> library into a new Rails Application check this out first.
> 
> - Is the session being set properly? Check your controllers. If you  
> cannot find a place where @session[‘user’] = you have a  
> problem (Note: this may have come with the library code)
> 
> - Is the browser/test case your using preserving sessions between calls?  
> I cannot tell you the number of times a stale browser cache in Fire Fox  
> or a test case that is not session aware has blown my whole day.
> 
> As a final thought, a lot of these data persistence problems are very  
> difficult to track down because there is no way to trace the execution  
> path through the program. We MUST wait for the client to make a second  
> request with the right data. To help me with this I have a debug  
> template that I use for all my pages. The template contains a button at  
> the bottom that will pop up a window which lists debug(@user),  
> debug(params), debug(session), and debug(flash). Very helpful for these  
> types of things.
