# Help - accessing session data from my model

**URL:** <https://www.ruby-forum.com/t/help-accessing-session-data-from-my-model/129581>\
**Category:** Rails\
**Created:** [February 19, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/help-accessing-session-data-from-my-model/129581 "2008-02-19T20:58:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [February 19, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/help-accessing-session-data-from-my-model/129581/1 "2008-02-19T20:58:18Z")

</div>

I have a model that contains data for many users/organizations. I need  
to limit the data by organization. I am trying to put this logic in  
the model vs the controller since many controllers/actions will be  
using that data. When I do that I get the following error:

undefined local variable or method `session’ for Itstaff:Class

this is the code for the model:

class Itstaff \< ActiveRecord::Base  
belongs\_to :location  
has\_many :servers  
has\_many :consoles  
def self.user  
find(:all, :conditions =\> [“location\_id in  
(?)”,session[:location\_ids]])  
end  
end

The same code works fine from the controller.

session[:location\_ids] gets initialized by the application controller  
during user log in. I tried to add an attr\_reader in application  
controller but then my application hangs completely. I appreciate your  
help.

---

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [February 20, 2008, 11:50am UTC](https://www.ruby-forum.com/t/help-accessing-session-data-from-my-model/129581/2 "2008-02-20T11:50:09Z")

</div>

On 19 Feb 2008, at 19:57, saljamil wrote:

> I have a model that contains data for many users/organizations. I need  
> to limit the data by organization. I am trying to put this logic in  
> the model vs the controller since many controllers/actions will be  
> using that data. When I do that I get the following error:
> 
> undefined local variable or method `session’ for Itstaff:Class

You can’t (session is basically an instance method of your  
controller). Models should be completely ignorant of the controller  
layer. You should pass down the appropriate value as an argument to  
the method defined in the model.

Fred
