# Find with conditions problem

**URL:** <https://www.ruby-forum.com/t/find-with-conditions-problem/132419>\
**Category:** Rails\
**Created:** [March 19, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/find-with-conditions-problem/132419 "2008-03-19T23:49:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 19, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/find-with-conditions-problem/132419/1 "2008-03-19T23:49:07Z")

</div>

Hi. I have tables for users and items. I’m trying to make a query that  
will find all of the items belonging to the current user.  
I have a testuser with id = 1 and this user has 4 items.

If I use the line:  
@items = Item.find(:all, :conditions =\> “user\_id = 1” )  
The four items are displayed no problem.

However if I use:  
@items = Item.find(:all, :conditions =\> “user\_id =  
‘session[:user][:id]’” )  
or  
@sid = session[:user][:id]  
@items = Item.find(:all, :conditions =\> “user\_id = @sid” )

No results are returned

I have tried printing out the value of session[:user][:id] on the  
results page and it displays correctly as 1.

Can anyone tell me where I’m going wrong?

Thanks.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 19, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/find-with-conditions-problem/132419/2 "2008-03-19T23:54:21Z")

</div>

> ‘session[:user][:id]’" )  
> or  
> @sid = session[:user][:id]  
> @items = Item.find(:all, :conditions =\> “user\_id = @sid” )

@items = Item.find(:all,  
:conditions =\> [“user\_id = ?”, session[:user][:id]])

-philip

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 19, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/find-with-conditions-problem/132419/3 "2008-03-19T23:59:05Z")

</div>

Thanks, if works now!
