4 ms·
> use nested routing for a cleaner design I think this is actually quite debatable and I've seen a lot of people argue that nested resources are an anti-patter
by shangxiao 12y ago
> use nested routing for a cleaner design
I think this is actually quite debatable and I've seen a lot of people argue that nested resources are an anti-pattern. I only ever use nested resources myself when the child resource is existentially dependent on the parent.
- madeofpalk 12y agoI think what he's describing, or at least what I do, is when you get all albums belonging to an artist. GET /artist/8/albums { albums: [89, 93] } GET /albums/89 { album: { id: 89, title: 'Blah' } }
- shangxiao 12y agoIn this case I would either make albums a field of artist, or just leave it off altogether and just filter /albums based on the artist foreign key.
- moron4hire 12y agoWhat about collaborations? The nested hierarchy implies a directed tree structure, but the relationships are really a graph. And I've personally found no more insidious of a problem in data design than trying to shoehorn a graph into a directed tree.
- hawleyal 12y agoThe relationships in URIs are abstractions anyway. The data is not actually stored at /artist/8/albums or /albums?artist=8. Both are abstractions of whatever underlying system is used to store the relationships.
- danielnaab 12y agoMore flexible: GET /albums?artist=8 GET /albums?artist=8&artist=10 GET /albums?title='Blah'
- hawleyal 12y agoAgreed. Nested routing is fine, as long as it doesn't become redundant. Do: /artist/8/albums /albums/9 Don't: /artist/8/albums/9 Using query filters versus nesting are virtually synonyms. I would say there is a minor semantic distinction in that the first one is a definite relationship, and the second one is a possible relationship. That is, "get the albums for artist 8" versus "get the albums searching for artist 8". The search could include other parameters as well, making it not as definite. /artist/8/albums /albums?artist=8
- shangxiao 12y agoChances are you're (well at least with me) going to do filtering anyway so /artist/8/albums becomes redundant ;)
- hawleyal 12y agoI see what you're saying, but it's not redundant, it's just a synonym. By redundant I meant that there are keys that are not necessary. /artist/{artist}/albums /albums?artist={artist} AlbumsController#index(artist)