[LRUG] Embarrassing Newbie question

Tom Stuart tom at experthuman.com
Mon Aug 13 12:00:05 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.

The short answer is that, no, there isn't a 1:1 relationship between  
controllers and models -- it's totally up to you how you decide to  
arrange them and what logic you choose to put into each controller.  
This'll always be the case.

The longer answer is that newer versions of Rails encourage a  
particular style of application architecture, known as REST, which  
(very approximately) entails putting your controllers and models into  
1:1 correspondence. In a so-called "RESTful" Rails application, each  
controller notionally exposes a collection of "resources" which  
support the basic CRUD actions and not much else, and typically the  
data for each such resource will live in an ActiveRecord model. There  
are many payoffs and pitfalls, most quite speculative, but one  
immediate benefit is that such a design can greatly reduce the  
complexity of your controller logic, helping to banish complex "junk"  
actions and giving you a better sense of clarity about what's  
actually going on. (Often the complexity has actually just moved  
elsewhere, but that's another story.)

It's a big topic but playing around with Rails' built-in  
scaffold_resource generator should give you an idea of how this  
works, and googling for "rest rails" is a good start too. LRUG even  
had a big argument about REST a few months ago, immortalised at  
http://skillsmatter.com/menu/650 for your viewing pleasure, but  
that's probably too pedantic to be immediately useful.

> Apols if this is the stupidest question ever asked...

It's not a stupid question at all; this is a difficult and  
contentious area with no easy answers. Experiment and see what works  
best for you!

Cheers,
-Tom



More information about the Chat mailing list