dingbat/nsrails

remoteCreateViaObject

pareeohnos opened this issue · 4 comments

Me again :P

So I want to create a remote object, and obviously there is the remoteCreate method, but this doesn't take into account nested resources. If for example, (like my previous issue) I have a user, which has many friends, I can easily add a new User resource which is great, but adding a friend becomes a little trickier. In rails, my route for creating the friend would look like

POST /users/:user_id/friends

Instead of

POST /friends

Because just /friends doesn't give any relation to the object it is associated with. I could supply the ID with the friend object, but this doesn't feel very nice. Given there is the ability to fetch objects via another object, shouldn't there be the ability to create them as well?

This is where you'll want to look at overriding

- (NSRRemoteObject *)objectUsedToPrefixRequest:(NSRRequest *)request

See here for details.

Assign the Friend the User object you want to nest inside, and return that object in this method via overriding.

Ahh awesome stuff! I really should dig a little deeper into the documentation before raising an issue :)
Out of curiosity, how come there are methods for fetching via an object, but not creating?

I suppose it's possible, you're right. But like we saw before, there becomes a point where once you add every possibility of each API from every angle, you start having to support an entire matrix of operations, which becomes especially bad if either another API or "angle" gets added. Why not deleting via an object, updating via an object? :)

Ha yeah I see your point :P