# Access controller var within model

**URL:** <https://www.ruby-forum.com/t/access-controller-var-within-model/53362>\
**Category:** Rails\
**Created:** [February 2, 2006, 11:49am UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362 "2006-02-02T11:49:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 11:49am UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/1 "2006-02-02T11:49:15Z")

</div>

I need to access params[:field\_name] from within my Model.  
@params[:field\_name] isn’t working?

---

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 11:54am UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/2 "2006-02-02T11:54:34Z")

</div>

Brendon G. wrote:

> I need to access params[:field\_name] from within my Model.  
> @params[:field\_name] isn’t working?

Why would you need to access that??

There is probably a better way to do it

Mikkel

---

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/3 "2006-02-02T12:01:31Z")

</div>

Mikkel B. wrote:  
…

> Why would you need to access that??
> 
> There is probably a better way to do it  
> …

I got it : field\_name \<-- local var

I have a complicated validate\_on\_create function i need it fot…

---

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/4 "2006-02-02T13:26:14Z")

</div>

Jarkko L. wrote:  
…

> That smells like very tight coupling between view and model. @params  
> hash is something that belongs to the controller and individual http  
> requests, not your model objects. Remember that AR is a totally  
> distinct entity in Rails, and doesn’t know anything about what is  
> happening on the controller/view side. If you got something you need to  
> pass to the AR models, create a method where you can pass the value of  
> the current params[:field\_name].  
> …

Sorry, I just needed to access the AR object, I thought i needed to use  
@params[]  
but I soon figured out what you mentioned above and was able to access  
‘field\_name’  
as a local var, This clarifies things quite alot. thx.

---

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/5 "2006-02-02T12:07:49Z")

</div>

On 2.2.2006, at 12.54, Mikkel B. wrote:

> Brendon G. wrote:
> 
> > I need to access params[:field\_name] from within my Model.  
> > @params[:field\_name] isn’t working?

It isn’t, and it shouldn’t.

> Why would you need to access that??
> 
> There is probably a better way to do it

That smells like very tight coupling between view and model. @params  
hash is something that belongs to the controller and individual http  
requests, not your model objects. Remember that AR is a totally  
distinct entity in Rails, and doesn’t know anything about what is  
happening on the controller/view side. If you got something you need  
to pass to the AR models, create a method where you can pass the  
value of the current params[:field\_name].

//jarkko

---

<div class="post-metadata">

**Author:** ![Brendon\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Brendon\_G](https://www.ruby-forum.com/u/Brendon_G)\
**Post date:** [February 2, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/access-controller-var-within-model/53362/6 "2006-02-02T18:34:43Z")

</div>

I forgot which one … but one Login\_system has Model constraints

–  
Rgds,  
–Siva J.  
[http://www.varcasa.com/](http://www.varcasa.com/)  
My First Rails Project.  
Education Through Collabration
