# Has\_many with :finder\_sql question

**URL:** <https://www.ruby-forum.com/t/has-many-with-finder-sql-question/51775>\
**Category:** Rails\
**Created:** [January 6, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/has-many-with-finder-sql-question/51775 "2006-01-06T22:22:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [January 6, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/has-many-with-finder-sql-question/51775/1 "2006-01-06T22:22:10Z")

</div>

hi all,

I have a Class, Client, which has\_many projects (Project class). the  
projects depend on a session\_id variable, however. So the question  
is, how can I do the following:

has\_many :projects,  
:finder\_sql =\> “SELECT p.\* FROM projects p INNER JOIN  
projects\_users pu ON pu.project\_id = p.id WHERE pu.user\_id =  
#{session[:user\_id]}”

The problem is that I need to filter a client’s projects using a  
session variable, and the :finder\_sql produces the error:

“undefined local variable or method `session’ for Client:Class”

I thought that I could be sure the variable existed be creating a  
default one in the initialize definition in the Client class, but that  
doesn’t work either.

Thanks!

rich

---

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [January 7, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/has-many-with-finder-sql-question/51775/2 "2006-01-07T22:58:53Z")

</div>

Rich B. wrote:

> session variable, and the :finder\_sql produces the error:
> 
> “undefined local variable or method `session’ for Client:Class”
> 
> I thought that I could be sure the variable existed be creating a  
> default one in the initialize definition in the Client class, but that  
> doesn’t work either.

In general, Models shouldn’t be aware of sessions. The session accessor  
is a controller method.

I’m having trouble relating your SQL to your description - is it User or  
Client that has the Projects, and is it has\_many (which wouldn’t require  
a join table) or has\_and\_belongs\_to\_many (which would)?

Suppose a User has\_many Projects, and you have the user id in the  
session under the key :user\_id.

If all you need to do is get the current user’s projects, you should be  
able to do something like this (untested) in a controller action:

@user = User.find(session[:user\_id])  
@projects = @user.projects

You don’t need to specify the SQL in the model.

If you are worried about making two queries rather than one, you can use  
the :include option to tell the find to include projects.

If this has to happen in many different actions, consider putting it in  
a before filter in the controller.

Another point that may be useful - in the current version of Rails you  
can also use find methods on collections, e.g.

@project = @user.projects.find(proj\_id)

This ensures that a user only sees his/her own projects, even if they  
tamper with the id being passed back from the browser.

regards

Justin
