# Controller and model strategies

**URL:** <https://www.ruby-forum.com/t/controller-and-model-strategies/108872>\
**Category:** Rails\
**Created:** [July 8, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/controller-and-model-strategies/108872 "2007-07-08T14:35:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Gunnarsson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@John\_Gunnarsson](https://www.ruby-forum.com/u/John_Gunnarsson)\
**Post date:** [July 8, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/controller-and-model-strategies/108872/1 "2007-07-08T14:35:30Z")

</div>

Hi,

I’m very new to ROR and have aquestion about which stregy to use for  
using controllers.  
Lets say i have 3 classes:

order  
order\_lines  
customer

I would like to build an application where i can create customer.  
create orders (which has belongs to an customer) and add orderlines  
(which belongs to an order)

Should i create one controller for each model (eg. order\_controller,  
customer\_controller…) or should I just crceate one  
ordermanagement controller that handles them all.

Please provide me with some pros and cons between the two options and  
which one you prefer, and way.

//John

---

<div class="post-metadata">

**Author:** ![John\_Gunnarsson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@John\_Gunnarsson](https://www.ruby-forum.com/u/John_Gunnarsson)\
**Post date:** [July 8, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/controller-and-model-strategies/108872/2 "2007-07-08T14:58:32Z")

</div>

I would do an AJAX based cart for this. In this case, since you’re not  
sending complete new page refreshes, but just updating parts of an  
existing page, you’d probably have a controller for orders, where you  
can expand / hide / delete / whatever for each order\_line. So, you  
would have a single controller for both order and order\_line (not  
order\_line\_s\_ class).

If, however, you don’t use any AJAX, and each click causes a different  
page to appear, and you want each order\_line to appear in its own  
page, you’d probably like to use REST for keeping your calls standard.  
Then, you’ll have an orders\_controller and under it (in the REST path  
hierarchy) you’d have an order\_lines controller.

Customers, being quite independent of the orders would probably get  
their own controller.

Amir

---

<div class="post-metadata">

**Author:** ![John\_Gunnarsson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@John\_Gunnarsson](https://www.ruby-forum.com/u/John_Gunnarsson)\
**Post date:** [July 8, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/controller-and-model-strategies/108872/3 "2007-07-08T15:03:33Z")

</div>

John Gunnarsson wrote:

> Hi,
> 
> I’m very new to ROR and have aquestion about which stregy to use for  
> using controllers.  
> Lets say i have 3 classes:
> 
> order  
> order\_lines  
> customer
> 
> I would like to build an application where i can create customer.  
> create orders (which has belongs to an customer) and add orderlines  
> (which belongs to an order)
> 
> Should i create one controller for each model (eg. order\_controller,  
> customer\_controller…) or should I just crceate one  
> ordermanagement controller that handles them all.
> 
> Please provide me with some pros and cons between the two options and  
> which one you prefer, and way.

Hi John,

I’m new to Rails too. I curious what replies you will get back.

From what I’ve seen, the general approach would be to have three models,  
three controllers, and probably three sets of views.

For example, with customer. You need to create, update, and manage a  
customer as its own entity.

There are concepts to an order that need to be managed without customers  
or order lines coming into play like marking it shipped or marking it  
over due, etc.

The book “Ruby by Rails” that you can by from the Pragmatic Programmer  
people goes through a shopping cart example that is exactly what you are  
doing (and that is just the front 1/3 or the book). By the time you are  
done, you have AJAX and all sorta of nifty things added It is a very  
good book and example.

Then the later part of the book are somewhat a reference (but not to  
replaced with rdoc).

And, the new toy in town is “REST”. It very much says that you would  
have three controllers. The book describes it a bit. There is a web  
cast that describe it too. It is a little controversial I suppose in  
that we have been doing “REST” all along. But the Rails “REST” implies  
some things and it turns out to help rethink your applications.

hth  
Perry
