Skip to content

Multiple relationships to the same model #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chieffancypants opened this issue Aug 15, 2014 · 3 comments
Closed

Multiple relationships to the same model #118

chieffancypants opened this issue Aug 15, 2014 · 3 comments
Assignees
Milestone

Comments

@chieffancypants
Copy link
Contributor

Have you given much thought as to how to define multiple relationships to the same model? For example:

GET /document/1

{
  id: 10,
  name: 'My Awesome Document',
  created_by: {
    id: 5,
    name: 'John Anderson'
  },
  modified_by: {
    id: 7,
    name: 'Wes Cruver'
  }
}

Both created_by and modified_by may relate to the users model, but as far as I can tell, there's no way to do this with the current way of defining relationships.

A backward-compatible way might be to allow an array:

DS.defineResource({
  name: 'document',
  relations: {
    belongsTo: [
      {
        user: {
          localField: 'created_by',
          localKey: 'userId'
        }
      },
      {
        user: {
          localField: 'modified_by',
          localKey: 'userId'
        }
      }
    ]
  }
}

Thoughts?

@jmdobry
Copy link
Member

jmdobry commented Aug 15, 2014

Yes, for some reason it never occurred to me that one might have multiple relationships of the same type to the same resource. Obviously, it won't work with the current implementation. Your suggested fix is exactly what would need to be done. Would you care to take a crack at it?

Hacking on angular-data is easy: clone it, npm install; bower install, then in one terminal: grunt go, then in another: grunt karma:dev.

I would maybe be able to get to this in 2 days.

@chieffancypants
Copy link
Contributor Author

Sure, I'll take a quick looksee

chieffancypants added a commit to chieffancypants/angular-data that referenced this issue Aug 18, 2014
chieffancypants added a commit to chieffancypants/angular-data that referenced this issue Aug 18, 2014
chieffancypants added a commit to chieffancypants/angular-data that referenced this issue Aug 19, 2014
chieffancypants added a commit to chieffancypants/angular-data that referenced this issue Aug 19, 2014
@jmdobry jmdobry self-assigned this Aug 20, 2014
jmdobry added a commit that referenced this issue Aug 20, 2014
issue #118: Allow multiple relationships to same model
@jmdobry
Copy link
Member

jmdobry commented Aug 20, 2014

Fixed by #122

@jmdobry jmdobry closed this as completed Aug 20, 2014
@jmdobry jmdobry added this to the 1.0.0-beta.1 milestone Aug 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants