# View Question... or is it a model question?

**URL:** <https://www.ruby-forum.com/t/view-question-or-is-it-a-model-question/153836>\
**Category:** Rails\
**Created:** [November 21, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/view-question-or-is-it-a-model-question/153836 "2008-11-21T17:45:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [November 21, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/view-question-or-is-it-a-model-question/153836/1 "2008-11-21T17:45:21Z")

</div>

I have a partial, that generates a list of categories. I’d like to  
use the same partial for two different scenarios where data will be  
coming from different models. In one case, the partial is grabbing  
category data directly from the Category model. In another case it’s  
grabbing it from an associated UserCategory model.

Based on the associations, in one scenario, i’d display the category  
name like this in the loop: category.name. In the other scenario  
(where I’m first retrieving category info through the UserCategory  
model, I’d display it like: category.category.name.

What’s the best way to use one partial for both situations? Do I  
alias category name somehow in the model so it can work for both  
scenarios?

I look forward to your suggestions.

Thanks!

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [November 21, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/view-question-or-is-it-a-model-question/153836/2 "2008-11-21T19:57:21Z")

</div>

That’s probably the ‘cleanest’ (since non-ugliest isn’t really a word)  
option. The next big question is how far do you take it? How you  
actually implement it is where some people tend to like one way over  
the other.

You could simply alias the name to a delegated method and call it done.

Or, like in my day job, we actually have ‘view state’ objects that  
take much this kind of complexity out of the controllers and models  
which makes things a bit simpler and easier to test. They are  
designed to be loaded up and validated (via Validatable[0] and a  
Forwardable[1] extension, if needed), which is then passed along to  
the view such that the view does not need to care what kind of  
“category” it has or how to get the name from it.

Hope that helps.

[0] [http://validatable.rubyforge.org/](http://validatable.rubyforge.org/)  
[1] [Jay Fields' Thoughts: Ruby: Forwardable addition](http://blog.jayfields.com/2007/02/ruby-forwardable-addition.html)

–  
James M.

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [November 21, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/view-question-or-is-it-a-model-question/153836/3 "2008-11-21T23:03:01Z")

</div>

Thanks James, that was very helpful.

-Alex
