Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:11:09+00:00 2026-05-15T14:11:09+00:00

Lets say I have a model similar to the one below, and I need

  • 0

Lets say I have a model similar to the one below, and I need to Fetch all ‘Person’ of a specific Company.companyName sorted by personRole.roleWeight

This is the model I have at the moment:

  • Entity: Company
  • Attributes: companyName
  • Relationships: companyRole

  • Entity: Role

  • Attributes: roleName, roleWeight
  • Relationships: rolePerson, RoleCompany

  • Entity: Person

  • Attributes: personName
  • Relationships: person Role

Here is a simple diagram of the relationship:

Company –< Role >–< Person

Is there a way to do this?
If I need to modify the model I would be happy to do so. All suggestions are welcome.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:11:10+00:00Added an answer on May 15, 2026 at 2:11 pm

    You can’t sort by role weight because it is possible to have more than one role that fits.

    You also can’t come at it from the Role (as opposed to the Person) because you have a many-to-many between role and person.

    You should re-think your design because having that many-to-many there does not make much sense. A little bit of data de-normalization, changing that many-to-many to a one-to-many and duplicating the rolename and roleweight values would solve the issue.

    Update

    Assuming you changed the design to:

    Company --< Role >-- Person
    

    Then the solution gets much easier:

    - (NSArray*)peopleSortedByWeightInCompany:(NSString*)company inManagedObjectContext:(NSManagedObjectContext*)moc
    {
      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      [request setEntity:[NSEntityDescription entityForName:@"Role" inManagedObjectContext:moc]];
      [request setPredicate:[NSPredicate predicateWithFormat:@"company.name == %@", companyName]];
    
      NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"weight" ascending:YES];
      [request setSortDescriptors:[NSArray arrayWithObject:sort]];
    
      NSError *error = nil;
      NSArray *roles = [moc executeFetchRequest:request error:&error];
      [request release], request = nil;
      NSAssert2(roles != nil && error == nil, @"Error fetching roles: %@\n%@", [error localizedDescription], [error userInfo]);
    
      NSArray *people = [roles valueForKeyPath:@"@distinctUnionOfObjects.person"];
    
      return people;
    }
    

    You basically fetch the Role entities sorted by weight and filtered by the company name. From that array of Role entities you then use KVC to gather all of the person objects on the other end of the relationship which will retrieve them in order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have model inheritance set up in the way defined below. class
Lets say I have a python model fibo.py defined as below: #Fibonacci numbers module
Lets say a have the following Model public class FirstModel { public List<SecondModel> SecondModels
So for example ... Lets say I have a Posts model and a News
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Lets say I have an image or two dimensional pattern similar to QRcode and
Lets say I have this model class Egg(models.Model): file = FileField(upload_to='media') img = ImageField(upload_to='media')
Lets say I have three django model classes - lets call them A, B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.