Tag Archives: xml

ObjectiveResource and Rails Relations

27 May

Today I came across the following; I have a model named “Candidate” and Candidates has many regions thru candidates_regions. Now wouldn’t it be handy that when you :GET a candidate you would also include all its relations in the xml or json document? It’s possible! And even by default. All you have to do on the rails side of your code is to make sure the relations get included.
You’d most likely have something in your Ruby code like this:

page.xml { render :xml => @model_name }

In order to also have relations included you can do the following

page.xml { render :text => @model_name.to_xml(:include => [:relation_one, :relation_two]) }

All there’s left now in your objective c interface is to include the relation which holds the interface and add the following:

#import "relationOne"

@interface modelName : NSObject {
    NSString *attribute;
    NSArray *relationOne;
}
@property (nonatomic, retain) NSArray *relationOne;
@end

The relationOne array will now hold objects of type RelationOne with all it’s set attributes, thanks to ObjectiveResource!

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: