# Two models one cotroller/view

**URL:** <https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866>\
**Category:** Rails\
**Created:** [March 4, 2008, 12:38am UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866 "2008-03-04T00:38:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [March 4, 2008, 12:38am UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866/1 "2008-03-04T00:38:13Z")

</div>

hi all

I have two different models representing two different data sets

but I want to use the same controler and views to process them

how can I DRY this up so I don’t have to use a separate controller and  
view  
for each model?

would helpers be the solution?

so their urls’s would work like this:

> **[Overview](https://www.salesforce.com/products/platform/overview/?d=70130000000liBh&internal=true&bc=HA)**
>
> Go digital fast and empower your teams to work from anywhere. Develop scalable, custom business apps with low-code development or give your teams the tools to build with services and APIs.

> **[Overview](https://www.salesforce.com/products/platform/overview/?d=70130000000liBh&internal=true&bc=HA)**
>
> Go digital fast and empower your teams to work from anywhere. Develop scalable, custom business apps with low-code development or give your teams the tools to build with services and APIs.

cheers

dion

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [March 4, 2008, 12:50am UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866/2 "2008-03-04T00:50:33Z")

</div>

On Mar 4, 12:30 am, “Dion H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi all
> 
> I have two different models representing two different data sets
> 
> but I want to use the same controler and views to process them
> 
> how can I DRY this up so I don’t have to use a separate controller and view  
> for each model?

Wasn’t it easier to DRY up your models instead of controllers and  
views?

If those data sets use logic that is close enough to require identical  
views, chances are you could merge those models with an extra column  
or two for identifying the kind. Just a wild guess, of course, since  
you gave us only so much details.

Otherwise, I think with the URLs you have posted, you may not need  
helpers. Use the model1/model2 part as an extra parameter, and process  
that in the controller to get the right records. (Sorry if I’m not  
making any sense).

Kind regards,

Branko

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [March 4, 2008, 1:16am UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866/3 "2008-03-04T01:16:56Z")

</div>

Hi Branko…

you made perfect sense…I have thought about those two options as well,

using the same model would be easy, but I thought it might be cleaner to  
have two different models, for the two slightly different  
datasets…(normalisation)

but testing for the model name as a parameter in the route would be  
should  
be just as easy…

however out of the three options…i’m not sure which one is the ‘rails’  
way  
to do it…

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [March 4, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866/4 "2008-03-04T12:16:06Z")

</div>

On Mar 4, 1:09 am, “Dion H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> however out of the three options…i’m not sure which one is the ‘rails’ way  
> to do it…

Well, I’m quite new to Rails so I wouldn’t know what exactly would be  
the true Rails way. But I guess the less you fight Rails, the closer  
your are to Rails way. 😉

Branko

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [March 4, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/two-models-one-cotroller-view/130866/5 "2008-03-04T16:39:34Z")

</div>

I don’t know if it could help in your case but you could do single-  
table inheritance :  
(see [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html) )

to put things simply, you add a column named “type” (with datatype set  
to string) in the table that will contain both models  
then, in the general model file:

class GeneralModel \< ActiveRecord::Base

# code goes here

end

class SpecializedModel1 \< GeneralModel

end

class SpecializedModel2 \< GeneralModel

end

then if each specialized model has some really unique properties, you  
can wrap these in a column that you would then serialize into a  
specific object type, which could be a Hash or anything else  
(serialization is also covered in the article I provided the link  
for).

If this doesn’t help in any way, check out :  
[http://api.rubyonrails.com/classes/ActionController/Resources.html](http://api.rubyonrails.com/classes/ActionController/Resources.html)  
it contains documentation for “resource”, and “resources” methods,  
which accept the “controller” and “path\_prefix” parameters you could  
find use for.
