In my iPhone app I'm communicating with the web application on rails that receive json files from the specific database tables. I have a three tables Users, UserWearers and Wearers with the relationships like below:
class User < ActiveRecord::Base
has_secure_password
has_many :user wearers
has_many :wearers, :through => :user wearers
end
class Userwearer < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :user
belongs_to :wearer
end
class Wearer < ActiveRecord::Base
has_many :user wearers
has_many :users, :through => :user wearers
end
Only Users and Wearers tables receive json files that I can map into my Core Data model. The Userwearer table contains rows like: user_id, wearer_id, created_at, updated_at, and doesn't receive any json response. Im totally amateur of mapping objects so please be understanding if I ask you some stupid question :) If I understand correctly it is kind of many to many relationship What Ive done in my app is the UserAuthentication and displaying wearers in UITableViewController with configured NSFetchResultsController delegate methods. Everything working ok till the moment when I'm trying to sign out the user and log in with the other credentials. In that case the UITableViewController display data of the previous and the current user. It seems to me that I should just add to the existing fetchedResultsController delegate method NSPredicate with info about displaying only data from the current user, and once the user is signing out just to delete current user from the core data with the related entities. Am I right?
So know I have to map relationships between these 3 tables. Is there any way to do this without information from the table UserWearers? How can I properly map relationships between them Or maybe there is some other way to fix my issue?
+(RKMapping *)usersMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Users" inManagedObjectStore:[[EdisseDateModel sharedDataModel]objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"user_id",
@"address1": @"address1",
@"created_at":@"created_at",
@"updated_at": @"updated_at",
@"email": @"email",
@"name":@"name",
@"password_digest": @"password_digest",
@"phone_no": @"phone_no",
@"postcode":@"postcode",
@"remember_token":@"remember_token",
@"user_type": @"user_type",
}
];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"userwearer" toKeyPath:@"userWearers" withMapping:[MappingProvider userWearersMapping]]];
[mapping setIdentificationAttributes:@[@"user_id"]];
return mapping;
}
/**
Return Mapping for entity named : UserWearers
@return RKEntityMapping object
*/
+(RKMapping *)userWearersMapping{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"UserWearers" inManagedObjectStore:[[EdisseDateModel sharedDataModel]objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"userWearer_id",
@"created_at":@"created_at",
@"updated_at":@"updated_at",
@"user_id":@"user_id",
@"wearer_id":@"wearer_id"
}
];
[mapping addConnectionForRelationship:@"wearer" connectedBy:@{@"wearer_id": @"wearer_id"}];
[mapping addConnectionForRelationship:@"user" connectedBy:@{@"user_id": @"user_id"}];
[mapping setIdentificationAttributes:@[@"userWearer_id"]];
return mapping;
}
/**
Return Mapping for entity named : Wearers
@return RKEntityMapping object
*/
+ (RKEntityMapping *)wearersMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Wearers" inManagedObjectStore:[[EdisseDateModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
@"id":@"wearer_id",
@"at_risk": @"at_risk",
@"created_at": @"created_at",
@"dob": @"dob",
@"status":@"status",
@"updated_at":@"updated_at",
}
];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"userWearers" toKeyPath:@"userWearers" withMapping:[MappingProvider userWearersMapping]]];
[mapping setIdentificationAttributes:@[@"wearer_id"]];
return mapping;
}