[LRUG] REST and Associations

Jonas Bengtsson jonas.b at gmail.com
Wed Feb 21 15:42:44 PST 2007


Hi,

I'm no RESTful in Rails expert by any means, but I'll give it a go.

1) I don't see any need for there to be a one-to-one mapping of models 
and RESTful controllers. The controllers are the way the app exposes 
itself to the web. The models are an implementation detail. For some 
models there's a natural one-to-one mapping to a controller, which means 
less work. When there isn't a one-to-one mapping you need to do a little 
more work in the controller, but that doesn't seem like a reason to 
compromise in the way your app works.

Having said that, the resource generator creates a model as well, I'm 
not entirely sure why that is.

2) How do you handle validation errors in your "create" action? Don't 
you render the "new" action? If so, I don't see that much of a 
difference, doing the same thing but for your select boxes.

Cheers,
  Jonas

Jonathan Leighton wrote:
> So I've been playing with REST lately and it all seems pretty spiffy but
> I am a little confused on the issue of associations.
> 
> Issue #1
> --------
> 
> We have a large database with a lot of tables and a lot of
> relationships. The client wants to be able to create a new resource, and
> said resource has lots of required associations. It would not make sense
> to create these associations separately to the original resource so that
> is happening all in one form on one page. Is this okay? That doesn't
> make me a bad person in DHH's eyes does it?
> 
> Issue #2
> --------
> 
> Similar to #1. Said resource has a has_and_belongs_to_many association.
> In the non-RESTful version, these were selected from select boxes. When
> creating a new resource, you'd be presented with one select box. If you
> wanted another you could click "add another". This would post the page
> back to the "new" action, which would recognise which submit button had
> been pressed and redraw the form with one more select box (and therefore
> association).
> 
> This is all fine and dandy but in the happy land of REST the form gets
> posted to the "create" action. I guess the fundamental problem is
> actually that in order to do the same thing I'd have to allow the create
> action to re-show the form with the additional select box(es). That
> would surely be evil and if DHH didn't think I'm a bad person for #1 he
> surely would now.
> 
> At the moment I am handling it by having an #add_foo action and calling
> that via ajax, which inserts the new select box. This is blatently evil
> in a progressive enhancement sorta way but seems less evil in a RESTful
> way.
> 
> Either way I am evil though. Help!
> 
> Thanks :)
> 




More information about the Chat mailing list