# Find in controller or view

**URL:** <https://www.ruby-forum.com/t/find-in-controller-or-view/190458>\
**Category:** Rails\
**Created:** [June 20, 2010, 10:33pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458 "2010-06-20T22:33:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norm\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@Norm\_S](https://www.ruby-forum.com/u/Norm_S)\
**Post date:** [June 20, 2010, 10:33pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458/1 "2010-06-20T22:33:32Z")

</div>

I have an app that does a number of collection\_selects.&nbsp; There are two general methods of getting the data and displaying it that I am aware of.  
  
You can do a find in the controller and assign the result to an instance variable and do the collection\_select stuff in the view.  
  
Alternatively you can put the whole find and collection\_select in the view (in erb of course).  
  
To me it seems to clutter up the view to do the find in the view so I prefer it in the controller.&nbsp; Is there some reason that it would be better to do it all in the view?   
  
I am looking for opinions here on the 'best practice' or does it not matter?  
  
Thanks  
Norm

–   
  
You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
  
To post to this group, send email to  
removed\_email\_address@domain.invalid.  
  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.

For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en.](http://groups.google.com/group/rubyonrails-talk?hl=en.)

---

<div class="post-metadata">

**Author:** ![Norm\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@Norm\_S](https://www.ruby-forum.com/u/Norm_S)\
**Post date:** [June 21, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458/2 "2010-06-21T15:31:17Z")

</div>

Norm S. wrote:

> Is there some reason that it would be  
> better to do it all in the view?

No.

Ideally, the view is only the presentation layer.

Among other reasons, when you start reusing partials, it will be come  
readily apparent why you want to marshal the data in the controller, and  
make it available to the view.

---

<div class="post-metadata">

**Author:** ![Norm\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@Norm\_S](https://www.ruby-forum.com/u/Norm_S)\
**Post date:** [June 22, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458/3 "2010-06-22T17:39:51Z")

</div>

Ar Chron wrote:

> Norm S. wrote:
> 
> > Is there some reason that it would be  
> > better to do it all in the view?
> 
> No.
> 
> Ideally, the view is only the presentation layer.
> 
> Among other reasons, when you start reusing partials, it will be come  
> readily apparent why you want to marshal the data in the controller, and  
> make it available to the view.

Right. The view never, ever, ever touches the database. If you think  
you want it to do so, you’ve got a design problem.

## Best,

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

---

<div class="post-metadata">

**Author:** ![Norm\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@Norm\_S](https://www.ruby-forum.com/u/Norm_S)\
**Post date:** [June 22, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458/4 "2010-06-22T17:49:45Z")

</div>

a tip when using partials: look into :locals option and use it  
extensively. Don’t assume that a partial can “see” things from the  
controller - specify what you want the partial to see using :locals.  
It’ll pay off in the end.

---

<div class="post-metadata">

**Author:** ![Norm\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@Norm\_S](https://www.ruby-forum.com/u/Norm_S)\
**Post date:** [June 22, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/find-in-controller-or-view/190458/5 "2010-06-22T18:27:38Z")

</div>

chewmanfoo wrote:

> a tip when using partials: look into :locals option and use it  
> extensively. Don’t assume that a partial can “see” things from the  
> controller - specify what you want the partial to see using :locals.  
> It’ll pay off in the end.

A most excellent tip. :object and :collection are also very helpful.

## Best,

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