# Accessing Models via Views

**URL:** <https://www.ruby-forum.com/t/accessing-models-via-views/165920>\
**Category:** Rails\
**Created:** [April 19, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920 "2009-04-19T22:35:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/1 "2009-04-19T22:35:24Z")

</div>

Hi

When creating a view to display infromation obtained from a model,  
should the view always get this information from the controller, or  
should it get it directly from the model?

For example, say I have Class and Student models, and they are in a  
many to many relationship via the enrolled table. Now my view wants to  
display the students of a given class. Should the controller run the  
query and store it in a variable (say @students) that is passed to the  
view? Or, should the view just access it directly like

\<% for enrolled in @class.enrolled\_students %\>  
\<%= enrolled.student.name %\>

I like the second way because it does not require me to put any code  
in the controller. I just feel like this is coupling the view and to  
the model and I can’t tell if that is a bad thing in an MVC framework?  
I feel like it is.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/2 "2009-04-19T23:39:13Z")

</div>

removed\_email\_address@domain.invalid wrote:

> When creating a view to display infromation obtained from a model,  
> should the view always get this information from the controller, or  
> should it get it directly from the model?

Which one is simplest?

I think it’s ideal when the controller sets up a single variable, such  
as  
@datagram, and then the view uses only first-level method calls on  
@datagram:

@datagram.marbles.each do |marble|  
\<%=h marble.name %\>  
end

Simplified eRB, of course, but you get the idea. We did not call  
@datagram.method.method(42).method.marbles. The model and controller  
should have  
set any parameters up for @datagram to do its job, such as provide the  
42.

(.each is technically a method call, but it’s more stable, and it’s  
essentially  
a part of Ruby’s core mechanics, so we give it a pass. We would not give  
.select{} or .inject{} the same pass!)

> For example, say I have Class and Student models, and they are in a  
> many to many relationship via the enrolled table. Now my view wants to  
> display the students of a given class. Should the controller run the  
> query and store it in a variable (say @students) that is passed to the  
> view? Or, should the view just access it directly like
> 
> \<% for enrolled in @class.enrolled\_students %\>  
> \<%= enrolled.student.name %\>

I can think of no reason to use ‘for in’ in Ruby. I suspect Matz added  
it early,  
to imitate languages that must use ‘for’…

But your enrolled.student.name (is missing an h, and) has too many dots.  
I  
suspect that would violate something called the “Law of Demeter” if any  
of those  
dots took a “left-turn” into another high-level module.

Because you will find enrolled delegating to student frequently, your  
code is  
already more dry if enrolled has a def name; student.name; end. That  
hides a dot  
in the model, which is the ultimate goal of refactoring - dense models,  
aggressive but sparse controller actions, and thick views with minimal  
Ruby  
method calls.

The controller is like the Government. Paraphrasing Abe Lincoln, it  
should only  
do what the model and view can’t do for themselves.

> I like the second way because it does not require me to put any code  
> in the controller. I just feel like this is coupling the view and to  
> the model and I can’t tell if that is a bad thing in an MVC framework?  
> I feel like it is.

Coupling the view to the model is part of MVC. Look up one of the  
diagrams - I  
think there’s an arrow from the V to M…

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2009, 1:34am UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/3 "2009-04-20T01:34:59Z")

</div>

Thanks Philip. Your comments have helped a lot.

I totally forgot about the Law of Demeter learned way back in second  
year CS. If I just follow that rule then I think most of my complaints  
about doing what I was in the view will disappear because I will only  
ever be using 1 level of indirection.

I will have to consult the pick-axe again on the nuances of iterators.  
I haven’t been using them because coming from a major C/Perl style  
background, for is just more familiar to me. Thanks for pointing this  
out though, no one else has yet.

As for the model \<=\> view in MVC, I was looking at ‘Design Patterns’  
and their is indeed a dotted line, where all the rest are solid lines.  
The book fails to distinguish the difference between the two though.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/4 "2009-04-20T18:38:20Z")

</div>

Thanks as well Rob. I like how you guys gave basically the same  
response at the exact same time. It really shows a lot of uniformity  
in the community. I was honestly expecting to get a lot of flame back  
about ‘do whatever is best’ or something to that effect.

On Apr 19, 5:37 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 21, 2009, 12:01am UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/5 "2009-04-21T00:01:01Z")

</div>

I tend to prefer code like:

@class.enrolled\_students.each do |student|  
student.name  
end

as Philip mentioned.

Enrolled students, if I understand your model(s) correctly, are just a  
specialized set of students. Not dropped\_students, or  
withdrawn\_students, or waitlisted\_students, or students\_on\_hiatus, but  
those currently enrolled in the class.

The “.each do |call it whatever|” gives you the chance to map the  
‘enrolled\_students’ back to their source, and pretty much anyone reading  
the code will make the leap (however small) that ‘name’ is an attribute  
or method of the ‘student’ class.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 21, 2009, 12:08am UTC](https://www.ruby-forum.com/t/accessing-models-via-views/165920/6 "2009-04-21T00:08:21Z")

</div>

Ar Chron wrote:

> I tend to prefer code like:
> 
> @class.enrolled\_students.each do |student|  
> student.name  
> end
> 
> as Phlip mentioned.

Tx - it reminded me to write up “Contractive Delegation” here:

[Radar – O’Reilly](http://broadcast.oreilly.com/2009/04/contractive-delegation.html)

I heard the term from an old Smalltalker, and it stuck…

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)
