[LRUG] Embarrassing Newbie question

Craig Webster craig at xeriom.net
Mon Aug 13 11:28:29 PDT 2007


On 13 Aug 2007, at 19:15, Ben Rooney wrote:
> Is there a 1:1 relationship between controllers and models? ie does
> every model have an associated controller, or can one controller
> control more than one model.

There doesn't need to be a controller for each model or a model for  
each controller. One controller can manage multiple models, but it'll  
add complexity to the controller and I'd suggest it should be  
avoided. One to one groups responsibilities nicely.

> I have an app I would like to build in RoR (quite heavy on Ajax and I
> don't fancy doing AJAX in PHP ...). It has about 20 or so models -
> the bulk of the work is going to be CRUD but there will be some cases
> of more complexity. So for example if I had a User class, and an
> organisation class which are going to be predominantly CRUD ops,
> could one controller do that, or do I need one for each?

I'd use one controller for each resource I wanted to manage with the  
application. If you wanted to work with the User model, I'd have a  
UsersController. If you wanted to manipulate an Organisation, that's  
an OrganisationsController.

The one model too one controller idea works well when playing with  
REST[1] and can help structure your application in a maintainable,  
easily understood manner. It's possible to fit most scenarios into  
the constraints provided by a RESTful architecture, but it can be a  
bit strange at first. What complexity beyond CRUD operations do you  
see in your application?

[1] http://peepcode.com/products/restful-rails

Cheers,
Craig
--
Craig Webster   | Lead Developer   | skype: craigwebster
Xeriom Networks | t: 0131 208 3800 | w: http://xeriom.net/

  -- Virtual machines, dedicated servers and colocation --

Xeriom Networks Limited.
94/1 Spring Gardens, Abbeyhill, Edinburgh, Scotland.
Registered in England and Wales. Company No. 5961686.

All agreements are made under our standard terms and conditions.
These can be found at http://xeriom.net/terms_and_conditions





More information about the Chat mailing list