# Multiple views of the same model

**URL:** <https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705>\
**Category:** Rails\
**Created:** [October 5, 2009, 10:08am UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705 "2009-10-05T10:08:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Jonas\_L](https://www.ruby-forum.com/u/Jonas_L)\
**Post date:** [October 5, 2009, 10:08am UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705/1 "2009-10-05T10:08:15Z")

</div>

Hi,

How can I implement two different views on the same model? My model  
has 30 attributes. The First view should display 1-12 and the second  
view the rest of them.

In my case I am dealing with two medical anamneses with different  
topics. My idea was to generate a model called anamnesis with every  
field name of the two topics. This model should be split into two views.

Regards  
Jonas

---

<div class="post-metadata">

**Author:** ![Jonas\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Jonas\_L](https://www.ruby-forum.com/u/Jonas_L)\
**Post date:** [October 5, 2009, 10:21am UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705/2 "2009-10-05T10:21:43Z")

</div>

2009/10/5 Jonas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi,
> 
> How can I implement two different views on the same model? My model  
> has 30 attributes. The First view should display 1-12 and the second  
> view the rest of them.
> 
> In my case I am dealing with two medical anamneses with different  
> topics. My idea was to generate a model called anamnesis with every  
> field name of the two topics. This model should be split into two views.

You could use a parameter in the url for the show action to indicate  
which view you want, then in the show action test the parameter and  
render the appropriate view.  
You could use a separate action in the controller.  
You could use a separate controller for the second view.  
If one view is showing some attributes and another only the rest (not  
clear from your post whether the second view shows all of them or  
not), then consider whether it should be a separate model.

Colin

---

<div class="post-metadata">

**Author:** ![Jonas\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Jonas\_L](https://www.ruby-forum.com/u/Jonas_L)\
**Post date:** [October 5, 2009, 10:48am UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705/3 "2009-10-05T10:48:04Z")

</div>

Have a look at Florian Hanke’s Representer plugin:

> **[GitHub - floere/representer: DEPRECATED: PLEASE SEE THE view\_models LINK BELOW.](https://github.com/floere/representer)**
>
> DEPRECATED: PLEASE SEE THE view\_models LINK BELOW. - GitHub - floere/representer: DEPRECATED: PLEASE SEE THE view\_models LINK BELOW.

It allows you to have ‘object oriented’ views, maybe it suits your  
needs…

Andi

---

<div class="post-metadata">

**Author:** ![Jonas\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Jonas\_L](https://www.ruby-forum.com/u/Jonas_L)\
**Post date:** [October 5, 2009, 10:55am UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705/4 "2009-10-05T10:55:05Z")

</div>

Thanks for the suggestions. I will try it.

Am 05.10.2009 um 10:46 schrieb Andi S.:

---

<div class="post-metadata">

**Author:** ![Jonas\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Jonas\_L](https://www.ruby-forum.com/u/Jonas_L)\
**Post date:** [October 5, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/multiple-views-of-the-same-model/176705/5 "2009-10-05T14:43:41Z")

</div>

Jonas L. wrote:

> Hi,
> 
> How can I implement two different views on the same model? My model  
> has 30 attributes. The First view should display 1-12 and the second  
> view the rest of them.
> 
> In my case I am dealing with two medical anamneses with different  
> topics. My idea was to generate a model called anamnesis with every  
> field name of the two topics. This model should be split into two views.

It sounds like you’re reinventing single-table inheritance here. Why  
not just use Rails’ single-table inheritance features?

Better yet, don’t try to put two classes in one table. Single-table  
inheritance works, but is problematic since it doesn’t really fit the  
relational model. Start with it, but consider refactoring to  
multiple-table inheritance.

> Regards  
> Jonas

## Best,

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