# Does passing the params collection to a model break mvc?

**URL:** <https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957>\
**Category:** Rails\
**Created:** [December 5, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957 "2008-12-05T16:10:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [December 5, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957/1 "2008-12-05T16:10:37Z")

</div>

I want to pass the params collection from the controller to the model to  
parse filtering and sorting conditions. Does having a method in the  
model  
that takes the params from the controller break MVC?

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [December 7, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957/2 "2008-12-07T22:02:13Z")

</div>

Not necessarily. The params object is just a hash. There is no reason  
why you can’t or shouldnt pass a configuration hash to a method. I do  
something similar quite often. One instance I find myself working with  
is XML API’s that I must generate. Many options are available on the  
query string. To handle this I use a pattern similar to the example  
below:

#Controller  
@models = Model.collection\_for\_api(params[:model])  
render :xml =\> @models

#Model  
def self.collection\_for\_api(opts = {})

# assemble a bunch of named scopes based on opts

end

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [December 8, 2008, 12:22am UTC](https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957/3 "2008-12-08T00:22:47Z")

</div>

You’re passing unnecessary knowledge to your model.  
It definitely doesn’t need to know the controller and/or the action  
it’s being called from. And this information is by default in the  
params hash of any given controller action.  
Thus, it’s considered a bad practice - bad design. Your objects should  
assume as little as possible about any other structure on your  
application.

–  
Leonardo Borges

> **[Leonardo Borges](http://leonardoborges.com/)**
>
> Code. Leadership. AI.

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [December 8, 2008, 6:11am UTC](https://www.ruby-forum.com/t/does-passing-the-params-collection-to-a-model-break-mvc/154957/4 "2008-12-08T06:11:09Z")

</div>

If you notice I only pass a section of the params has designed  
specifically to build the appropriate query, not the entire hash.
