20

I have the following structure in my app:

MessageRecipients -> Message -> User 
                     Message -> Activity

where the -> represent a pointer. I have separated out the messages and message recipients tables because I need to capture whether the message has been read by each recipient.

The query I want to do is find all of the messages for a recipient. So I need to include the message in the MessageRecipients query but I would also like to include the nested User and Activity objects.

var MessageRecipient = new Parse.Query('MessageRecipient');
messageQuery.equalTo('recipient', query.recipient);
messageQuery.include('message');

The message comes back without the activity and sender populated. I tried calling messageQuery.include('activity') but that didn't work as activity is not on the correct table.

Is there a way to do this query or do you have to do separate queries for the nested objects?

2 Answers 2

37

You'll need to include those sub-sub-objects as well:

messageQuery.include('message');
messageQuery.include('message.user');  // if user is the column name
messageQuery.include('message.activity'); // if activity is the column name
Sign up to request clarification or add additional context in comments.

5 Comments

Just a useful hint for anyone reading this answer - even though you can include sub objects using dot notation like this correct answer describes doesn't mean that's how you access to the object in your adapter. You will have to break up access like this: ParseObject activity = object.getParseObject("message").getParseObject("activity"); if you try ParseObject activity = object.getParseObject("message.activity"); if will give you a NullPointerException.
Lets say that i make a query to the MessageRecipients and i want this query to have ascending order from the class Activity (createdAt), is there any way to make this happen ?
Nope, you can only order based on columns in the base class for the query. Sounds like you'll have to do some sorting on the client side.
@MaxWorg thanks a lot! I think that should be included at the end of the answer. Ran into the same issue on Android :)
Hint for includeAll() in JS. It is not working with includeAll() as far as I tried but if you include like in example it works
19

You can also achieve this in one line:

messageQuery.include( ["message", "message.user", "message.activity"] );

1 Comment

great point! details like this are never alluded to the Parse docs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.