# Accessing model objects directly in the view

**URL:** <https://www.ruby-forum.com/t/accessing-model-objects-directly-in-the-view/108928>\
**Category:** Rails\
**Created:** [July 9, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/accessing-model-objects-directly-in-the-view/108928 "2007-07-09T17:01:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [July 9, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/accessing-model-objects-directly-in-the-view/108928/1 "2007-07-09T17:01:15Z")

</div>

Is there any general guidelines for accessing model objects directly  
in the view?

It seems like it would be a potential problem - that it would be  
better to keep model objects out of the view entirely. Instead, the  
controller should extract whatever data is needed from the model and  
put it into temporary objects, thus protecting the views from any  
changes in the models and keeping them from causing any problems by  
inadvertently calling things in the model.

On the other hand, this in effect creates a new structure just for  
passing data from controller to view, when the models already  
structure the data in question. That seems like duplicate effort, and  
not at all in the spirit of Rails.

Is there any common thought on this?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [July 9, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/accessing-model-objects-directly-in-the-view/108928/2 "2007-07-09T17:15:18Z")

</div>

> Is there any general guidelines for accessing model objects directly  
> in the view?

The best way to answer “general guidelines” type of questions is to  
look at the source.

In this case, look at what the scaffold\_resource generator does for you.

You’ll see lots of @mymodel and @mymodels throughout the views.

Using models, and collections of models, in the view is not only a  
Good Thing, but the alternative seems like madness to me.

The only time I use an abstraction layer is when allowing user-edited  
templates. For that kind of thing, look into Liquid and the way it  
wraps models in drops.

For general development, though, use your models. That’s why they  
are there.

Note: if you find yourself doing things like \<% @mymodel.destroy %\>  
that is Bad. But \<%= @mymodel.name %\> is good.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [July 9, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/accessing-model-objects-directly-in-the-view/108928/3 "2007-07-09T17:23:20Z")

</div>

Generally speaking, MVC states a View can ask a Model for information to  
display. However a View cannot ask a Model to perform Business-oriented  
operations. The Controller will retrieve requested Models for a View  
and  
via a View submission ask a Model to perform business operations. The  
Controller should not affect the display of a Model short of providing  
it to  
the View.

–Mel
