[LRUG] REST and Associations
Roland Swingler
roland.swingler at gmail.com
Thu Feb 22 14:02:30 PST 2007
Hi,
This is something I have been having a lot of conceptual problems with
as well - some similar to Jonathan's problems. I have only started
using the RESTful routes in the last couple of days or so and I posted
some questions to the ruby on rails talk mailing list, but got no
response - so as someone else has started the ball rolling...
1. From what I've read, I thought all urls were meant to be nouns in
restland - how does having /posts/1;edit give you any advantage over
/posts/edit/1 or /posts/1/edit ? A semicolon does not a noun make. For
me this highlights a possible weakness in thinking of everything as
nouns - see Steve Yegge's "Kingdom of Nouns" article -
http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html
2. What is the right way to do search? Are query strings still ok in
urls? Is search a seperate controller?
3. Similarly what is the right way to do sorting/pagination? For
example when I GET /posts I may want to only return the top ten in
html - but what about in the xml respond_to do I return all of them or
just ten? Is the idea that *exactly* the same resource(s) are
returned, no matter the mime type or can _what_ is returned vary as
well as _how_ it is returned?
4. The urls nested resources produces only seems to make sense for the
new / index /create actions - by the time the resource has a unique id
it already has a unique url, because the id is unique - but there
doesn't seem to be any way to disable the urls you don't want.
5. For nested resources, how do you avoid a dirty great switch
statement in your controller if things can be nested under many
different resources?
6. What do you do when you want to create multiple objects at once?
What do you return in the location field in the xml response? or
update or destroy many objects at once? What happens if you want to do
ANYTHING to more than one resource at once?
7. How useful is ActiveResource - is anyone here using it? can you do
much more than find a list of resources/resources by id - like the sql
options in find()?
Any thoughts on any of these very very welcome - and as a call for
talk ideas was raised earlier I would really appreciate a talk on REST
that goes beyond the basics of here is a resource generated with the
scaffold - "look now I can find a record with an id with
ActiveResource" - anyone offering...?
On a side note does anyone know if there is a group/mailing list
devoted to REST - I tried to find one the other day but google groups
came up blank :(
Cheers,
Roland
On 2/21/07, Jonas Bengtsson <jonas.b at gmail.com> wrote:
> 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 :)
> >
>
> _______________________________________________
> chat mailing list
> chat at lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
More information about the Chat
mailing list