# Where to place the session variable to access it in model

**URL:** <https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151>\
**Category:** Rails\
**Created:** [October 17, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151 "2008-10-17T12:49:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [October 17, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151/1 "2008-10-17T12:49:13Z")

</div>

Hi  
I have the model SDTicket and SDCi  
SDTicket  
has\_many :service\_desk\_cis

SDCi  
after\_save :update\_sd\_ticket  
belongs\_to :sd\_ticket  
def update\_sd\_ticket  
self.sd\_ticket.update\_attribute(“modified\_by\_id”,#here I need value  
from session)  
end

I have a user controller in that session[:id] = current\_user\_id #here I  
am setting current user id to session[:id]

```
 Could you please tell me how I can access this value from SDCi

```

class above in the call back after\_save

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [October 17, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151/2 "2008-10-17T13:47:26Z")

</div>

Sijo Kg wrote:

> Hi  
> I have the model SDTicket and SDCi  
> SDTicket  
> has\_many :service\_desk\_cis
> 
> SDCi  
> after\_save :update\_sd\_ticket  
> belongs\_to :sd\_ticket  
> def update\_sd\_ticket  
> self.sd\_ticket.update\_attribute(“modified\_by\_id”,#here I need value  
> from session)  
> end
> 
> I have a user controller in that session[:id] = current\_user\_id #here I  
> am setting current user id to session[:id]
> 
> ```
> Could you please tell me how I can access this value from SDCi
> 
> ```
> 
> class above in the call back after\_save

I can’t (personally). I would probably create an instance method in the  
model inside which I would save the model (self.save) and do other  
things like update the related model. If you do it this way you could  
also wrap it in a transaction block to make the whole thing atomic - at  
least you have that option anyway.

You’d pass the session value in as an argument to this method from the  
controller; your model shouldn’t need to know about where the  
current\_user\_id came from (ie someone’s http session). This makes  
things more testable / less coupled.

That’s just my thoughts.

–  
Daniel B.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [October 17, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151/3 "2008-10-17T15:18:26Z")

</div>

> ```
> self.sd_ticket.update_attribute("modified_by_id",#here I need value
> 
> ```

I get the feeling from this line that you are confusing the  
responsibilities of the Model-View-Controller. Yes, there has been much  
blogged about skinny controllers and fat models. However, this does not  
mean you should yank all of the controller’s responsibilities away from  
it.

In my understanding, the calling of “update\_attributes” method is a  
controller responsibility. Yes, it is the model that actually does the  
updating of the data but the "message’ to provide the values to be  
updated should be sent by a controller. It’s also the controller’s  
responsibility to maintain a user’s session, so it also has access to  
the session object.

This is how people get backed into the corner (or fall into the trap)  
where they begin asking how to access session data in a model. It’s the  
controller’s responsibility to decouple the model from the view and  
controller. When you start mixing these responsibilities is when you get  
yourself inot trouble.

Sijo Kg wrote:

> Hi  
> I have the model SDTicket and SDCi  
> SDTicket  
> has\_many :service\_desk\_cis
> 
> SDCi  
> after\_save :update\_sd\_ticket  
> belongs\_to :sd\_ticket  
> def update\_sd\_ticket  
> self.sd\_ticket.update\_attribute(“modified\_by\_id”,#here I need value  
> from session)  
> end
> 
> I have a user controller in that session[:id] = current\_user\_id #here I  
> am setting current user id to session[:id]
> 
> ```
> Could you please tell me how I can access this value from SDCi
> 
> ```
> 
> class above in the call back after\_save
> 
> Thanks in advance  
> Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [October 21, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151/4 "2008-10-21T14:16:39Z")

</div>

But Sijo needs is justified when tracing “who” is doing something to  
your model.

Sijo,

look into this screencast from Ryan B. ([http://railscasts.com/](http://railscasts.com/)  
episodes/119-session-based-model). It provides with good leads for a  
solution on what your looking.

In short what you want to provide your model with is an execution  
context which will contain the user id (not the whole session).

What you have to keep in mind is to avoid putting anything at the  
class level since it would prevent your code from bring Threadsafe  
(which Rails 2.2 is supposed to be).

Jean-Marc Lagacé  
[http://m2i3.com/blog/jean-marc](http://m2i3.com/blog/jean-marc)

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [October 22, 2008, 5:57am UTC](https://www.ruby-forum.com/t/where-to-place-the-session-variable-to-access-it-in-model/151151/5 "2008-10-22T05:57:00Z")

</div>

Hi  
Thanks for your reply  
Sijo
