# How to access variable in 3 level (model,controller,view)

**URL:** <https://www.ruby-forum.com/t/how-to-access-variable-in-3-level-model-controller-view/188294>\
**Category:** Rails\
**Created:** [May 9, 2010, 4:53pm UTC](https://www.ruby-forum.com/t/how-to-access-variable-in-3-level-model-controller-view/188294 "2010-05-09T16:53:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@Fuyi](https://www.ruby-forum.com/u/Fuyi)\
**Post date:** [May 9, 2010, 4:53pm UTC](https://www.ruby-forum.com/t/how-to-access-variable-in-3-level-model-controller-view/188294/1 "2010-05-09T16:53:48Z")

</div>

how to access variable in 3 level (model,controller,view)

i use session[:current\_unit] only can in C and V  
can’t access in M

i mean is use a goloble variable can access in anywhere

Such as:  
user slecet a Unit,  
in model, before User.save the user.unit\_id must set  
session[:user\_id]  
in login form User must select a unit , this is current\_unit, I  
save it in session[:user\_id]  
but how can i get it in model  
or there are an other mothed to handle it?

thanks!

---

<div class="post-metadata">

**Author:** ![Fuyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@Fuyi](https://www.ruby-forum.com/u/Fuyi)\
**Post date:** [May 9, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/how-to-access-variable-in-3-level-model-controller-view/188294/2 "2010-05-09T17:12:28Z")

</div>

Fuyi wrote:

> how to access variable in 3 level (model,controller,view)
> 
> i use session[:current\_unit] only can in C and V  
> can’t access in M
> 
> i mean is use a goloble variable can access in anywhere

You probably don’t want to use a global variable for this.

> Such as:  
> user slecet a Unit,  
> in model, before User.save the user.unit\_id must set  
> session[:user\_id]  
> in login form User must select a unit , this is current\_unit, I  
> save it in session[:user\_id]  
> but how can i get it in model  
> or there are an other mothed to handle it

It sounds like you’re not going about this correctly. Can you give more  
details of what you’re trying to do?

> thanks!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Fuyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@Fuyi](https://www.ruby-forum.com/u/Fuyi)\
**Post date:** [May 9, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/how-to-access-variable-in-3-level-model-controller-view/188294/3 "2010-05-09T21:20:09Z")

</div>

In declarative\_authorization you can do

```
  before_filter {|c| Authorization.current_user = c.current_user}

```

in ApplicationController, then call

```
  using_access_control

```

in Model, and you get current\_user(or something like that) in your  
model. Check how they do it.

Also, there is a hack from @lifo(Rails core team and 37signals member)  
[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)
