dingbat / nsrails

iOS/Mac OS framework for Rails
http://nsrails.com
MIT License
517 stars 37 forks source link

Fetching nested resources #44

Closed pareeohnos closed 11 years ago

pareeohnos commented 11 years ago

I might be missing something, but I can't quite figure out how to retrieve nested resources when they aren't returned in the original resources response. I have a user model, that is linked to other users. When a user logs into the app, it retrieves the user resource, but this does not provide the linked users. I then have a separate route setup to retrieve them, such as

/users/1/friends

How can I retrieve these? Obviously I can't do [User remoteAll] as this will just request /users but I can't seem to find anything explaining how this might be possible?

dingbat commented 11 years ago

+ (void) remoteAllViaObject:(NSRRemoteObject *)parentObject

So, something like:

[Friend remoteAllViaObject:user async:<block>];

:)

pareeohnos commented 11 years ago

Ahhh perfect! So I'd need to create a new model then for friends, even though it's the same model in the Rails app? I guess I could just extend the User model for simplicity though :)

dingbat commented 11 years ago

Ah, interesting. Yeah, making a Friend class that subclasses User (and is otherwise empty) is probably the best way to do this.

Actually, if you really wanted to stay within the User class there's a cool trick you can do with overriding objectUsedToPrefixRequest: (see here), but to be honest it'd probably be more trouble than its worth, and I'm not even sure what this would look like for class-level requests.

Of course, as I'm sure you know, you can also nest the friends array when serving the user record to your client.

pareeohnos commented 11 years ago

Cool that sounds reasonable. It makes the code a little clearer as well having separate models I guess. I was trying to avoid nesting the data in the original response, so this way should work perfectly :)

dingbat commented 11 years ago

Awesome, closing. Feel free to reopen if something comes up.