# Model Advice

**URL:** <https://www.ruby-forum.com/t/model-advice/126931>\
**Category:** Rails\
**Created:** [January 23, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/model-advice/126931 "2008-01-23T15:41:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/model-advice/126931/1 "2008-01-23T15:41:23Z")

</div>

Hi-

I have a single controller that’s associated with a single model. I  
would now like to create a whole bunch more models to use the same  
controller, is this a bad idea?

If I use the scaffold generator (which takes the controller and model  
as inputs) will this wipe out all the work I have already done for the  
first model (in the views and controller)?

What is the best way to go when you have a lot of models and a single  
controller?

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/model-advice/126931/2 "2008-01-23T16:46:23Z")

</div>

I’m guessing you want to perform some operation on multiple models in  
the same action.

You can use: script/generate model ModelName name:string this:integer  
that:boolean

This lets you generate all the extra models (and migrations). You’ll  
then need to add them into the controller yourself.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/model-advice/126931/3 "2008-01-23T17:24:04Z")

</div>

Nevermind…I looked it up, sorry for being lazy 🙂

So, a question on good practice:

Having multiple models in one controller is a good/normal thing?

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/model-advice/126931/4 "2008-01-23T16:55:47Z")

</div>

Yes, I will essentially do the same things with the new model.

What are the extra parameters doing in the generator?

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/model-advice/126931/5 "2008-01-23T17:25:16Z")

</div>

The extra (optional) arguments after the ModelName will prepopulate  
the generated migration file with the columns (and types) specified.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/model-advice/126931/6 "2008-01-23T17:52:53Z")

</div>

Ok. Thanks very much for your feedback!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 23, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/model-advice/126931/7 "2008-01-23T17:38:11Z")

</div>

There’s nothing inherently wrong with multiple models in a  
controller. Often times, though, you’re dealing with a single model  
and its associations (which are other models). For instance, finding  
a product and its category, brand, sizes, and colors. You only need:  
@product = Product.find(params[:id]) to get @product.brand,  
@product.category, @product.sizes, etc.

There’s a really good screencast on doing CRUD with associations here:

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 24, 2008, 12:27am UTC](https://www.ruby-forum.com/t/model-advice/126931/8 "2008-01-24T00:27:19Z")

</div>

I find it better, even though multiple controllers do very much the same  
thing, to have a separate controller for every model in the case that if  
I  
have to change something in one model I don’t have to split it up later  
on.

It’s also more along the lines of the Rails way.
