# can I access session variable in model

**URL:** <https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360>\
**Category:** rubyonrails-talk\
**Created:** [July 25, 2008, 6:16am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360 "2008-07-25T06:16:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 25, 2008, 6:16am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/1 "2008-07-25T06:16:13Z")

</div>

Hi &nbsp;&nbsp;&nbsp;can I access session variable in model? I have in the user controller code session[:site\_id] = @the\_site.site\_id

And how can i access this in one of my active record model SDAttachment?I tried like site\_id=session[:site\_id] but this gives error. Please help

Sijo

---

<div class="post-metadata">

**Author:** ![Thorsten\_Muller2](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@Thorsten\_Muller2](https://discuss.rubyonrails.org/u/Thorsten_Muller2)\
**Post date:** [July 25, 2008, 8:21am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/2 "2008-07-25T08:21:58Z")

</div>

no, you can't

If you need information from the session hash in a model, you must hand it to the models method like:

def do\_something(session\_id) ... end

and call that from the controller like

@my\_model.do\_something(session[:site\_id])

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 25, 2008, 1:58pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/3 "2008-07-25T13:58:17Z")

</div>

Thorsten Mueller wrote:

> no, you can't

Well, you \_can\_, but it's not as straight forward as you'd like. You can use Thread.current and store things similarly as you do with session. I don't know the ramifications of doing this with very much data, though. This thread

> **[Automating user-specific stamps on records](https://www.ruby-forum.com/t/automating-user-specific-stamps-on-records/139141)**
>
> So, given the opinion of Rails-core regarding the it’s-a-feature-not- a-bug lack of access of the session to models, what has been the “rails way” of dealing with these cases: – stamping records with created\_by and updated\_by fields with user ...

mentions it. A Google search will probably turn up a bit of useful information.

Peace, Phillip

---

<div class="post-metadata">

**Author:** ![Russell\_Norris](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Russell\_Norris](https://discuss.rubyonrails.org/u/Russell_Norris)\
**Post date:** [July 25, 2008, 2:00pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/4 "2008-07-25T14:00:50Z")

</div>

Not to mention it completely violates MVC. So even if you find out how... It's not a good idea.

RSL

---

<div class="post-metadata">

**Author:** ![sadathiru](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@sadathiru](https://discuss.rubyonrails.org/u/sadathiru)\
**Post date:** [July 26, 2008, 4:27pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/5 "2008-07-26T16:27:32Z")

</div>

Yes you could access the session variable in model. Paste the below code in application.rb

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 26, 2008, 4:41pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/6 "2008-07-26T16:41:30Z")

</div>

Hi &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lots of thanks for your help.It is working now..But what actaully happened? I have the curiosity to know that..Is it anything against the MVC architecture? And will it create any future problems during deployment like that?

Thanks Sijo

---

<div class="post-metadata">

**Author:** ![sadathiru](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@sadathiru](https://discuss.rubyonrails.org/u/sadathiru)\
**Post date:** [July 27, 2008, 1:37am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/7 "2008-07-27T01:37:53Z")

</div>

Hi, &nbsp;&nbsp;&nbsp;Probably no problem comes.I am not sure. Its your responsibilty. And you could access all these variables there &nbsp;&nbsp;&nbsp;["session", "cookies", "params", "request"] .

&nbsp;&nbsp;&nbsp;Thanks, &nbsp;&nbsp;&nbsp;Sadeesh.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 27, 2008, 3:30am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/8 "2008-07-27T03:30:05Z")

</div>

Hi --

> Hi &nbsp;&nbsp;&nbsp;&nbsp;Lots of thanks for your help.It is working now..But what actaully happened? I have the curiosity to know that..Is it anything against the MVC architecture? And will it create any future problems during deployment like that?

If you're talking about the code with "you\_dont\_have\_bloody\_clue" and so forth, don't use it. It was some kind of joke, I believe.

The model has no concept of a "session". It shouldn't be able to tell whether it's being accessed from a controller or the console or a standalone script. It's only the controller that knows about sessions.

As another respondent said, if you need the model to have session data, you should write your model method to take an argument and then pass the relevant session data in. The model doesn't know where the method argument comes from, and it shouldn't know.

David

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 28, 2008, 3:55am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/9 "2008-07-28T03:55:10Z")

</div>

Hi &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The code is not a joke .It is working anyway..And I tried as you said.(5 th post above)..In the model ServiceDeskAttachment i made a attr\_accessor :company And from controller I wrote @sd\_attachment=ServiceDeskAttachment.new(params[:attachment\_data]) @sd\_attachment.company=session[:company] @sd\_attachment.save

And in ServiceDeskAttacment model I I have the following code at top upload\_column :attachment, :store\_dir =\> proc{|inst, attachment| "uploads/servicedesk/#{@company}/#{inst.id}"}

But this does not get @company value...If I get value there, then my problem is solved..Could you please tell me how can this be solved?

Thanks in advance sijo

---

<div class="post-metadata">

**Author:** ![George\_Bailey](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@George\_Bailey](https://discuss.rubyonrails.org/u/George_Bailey)\
**Post date:** [July 28, 2008, 4:25am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/10 "2008-07-28T04:25:08Z")

</div>

The whole thing is just not a good idea. If your model needs external data, then pass it in. When classes have direct access to things that do not belong to them then you're building a house of cards.

If you have a model that needs some external data, then pass it in as an argument.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 28, 2008, 2:57pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/11 "2008-07-28T14:57:24Z")

</div>

Hi --

---

<div class="post-metadata">

**Author:** ![Pratik1](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@Pratik1](https://discuss.rubyonrails.org/u/Pratik1)\
**Post date:** [July 29, 2008, 1:57pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/12 "2008-07-29T13:57:10Z")

</div>

[http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model](http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model)

I don't think the message could have been any cleaerer. But if it helps. let me say it again as the original author of that code : DO NOT USE IT.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 29, 2008, 4:36pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/13 "2008-07-29T16:36:01Z")

</div>

Hi --

---

<div class="post-metadata">

**Author:** ![Pratik1](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@Pratik1](https://discuss.rubyonrails.org/u/Pratik1)\
**Post date:** [July 29, 2008, 5:04pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/14 "2008-07-29T17:04:36Z")

</div>

Hey David,

Sorry, I didn't mean to quote you ( just pressed gmail reply button ). My message was not for you, but for the people using the code 🙂

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 29, 2010, 5:37pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/15 "2010-06-29T17:37:46Z")

</div>

Hi,

I have a scenario where the session variable is handy in the model - I need to be able to get the current value, as it may be changed at any time.

I have a delayed\_job that runs a long running model activity (calling multiple web services, populating the database, etc.) in the background. This is triggered when a user selects an item from a list in the UI. However, if the user clicks on another item, I want the current processing to stop, as I want to keep the delayed\_job worker pool as free as possible (the more the jobs, the more I am billed by the hoster). The only way I can see to achieve this is to set a session variable (session[:current\_topic\_id]) when the user clicks on a topic, and then check in each stage of the model action if the current topic is the same as the one stored in the session, and abort the long job if they are out of sync.

Here, passing in the session variable to the model method wont really work for me, I have to pass in the session id and check the variable value across the model code.

Thanks Anand

Pratik Naik wrote:

---

<div class="post-metadata">

**Author:** ![Anand\_Ramanathan](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Anand\_Ramanathan](https://discuss.rubyonrails.org/u/Anand_Ramanathan)\
**Post date:** [July 1, 2010, 8:16pm UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/16 "2010-07-01T20:16:31Z")

</div>

I tried passing the session id around to the model, and agree that it is very ugly.

I am curious to know how you can best achieve this scenario without passing in the session variable: abort a delayed\_job (or other model based) work item when user changes something - this is mainly required because delayed job workers cost money at my host (heroku) and if the user randomly clicks around on the UI, I dont want to be billed for all the spawned jobs that no one cares about anyway.

Thanks

Anand

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 20, 2012, 4:40am UTC](https://discuss.rubyonrails.org/t/can-i-access-session-variable-in-model/32360/17 "2012-11-20T04:40:27Z")

</div>

We can do that with attr\_accessor

In model attr\_accessible :site\_id attr\_accessor :site\_id

in form, add a hidden field like this

=f.site\_id, value; session[:site\_id]

in model we can use self.site\_id now.
