Establish a hasMany relationship using a "foreign key"?

Summary

I have a bit of a problem using Ember-Model, trying to establish a unique relationship between two models.

Based on current responses that I have received here on S.O., Ember Forums, and #emberjs. I am beginning to believe that there is no built-in solution for this problem, and I am reformatting my question to specify what is needed.

Details

I am populating a template currently with a full set of debtor information. All the information comes from multiple calls to the server.

The first bit is the basic Debtor info. This part is easy because I can use the model hook and a dynamic segment to retrieve it.

My server returns a JSON for a Debtor… Here’s the short version:

{
  "debtor" = {
    "debtor_id": 1003,
    "debtor_name": Steve,
     //... more JSON
    "debtor_contact_id": 1345
  }
}

The dynamic segment for Debtor is filled with the value of the debtor_id, but also notice this debtor has a debtor_contact_id. Every Debtor record retrieved from the server has a unique debtor_contact_id. On the database, this value is a “foreign key” that will tell which contact table belongs to which debtor table.

There is no way to predict which contact info relates to which debtor without this key/value pair.

I currently have “Contacts” belongsTo “Debtor”, but that is not enough to do the job.

When it is time to fill the “Contacts” model. Ember-Model needs to know to build the value from debtor_contact_id into the ajax URL as a query parameter in order to GET the correct API.

I am still learning all of this stuff and so far I have not been able to fully follow any tutorials because my use case has an extra step needed somewhere.

This is the expected behavior I am hoping to see:

  1. Model hook will work as expected to pull the specific debtor and put it into a “debtor” model (this part is currently working just fine)
  2. Somehow “debtor_contact_id” is read from the payload
  3. that value is added as part of a server query to find a separate API
  4. the resulting contact info will be pulled into a “contact” model
  5. hopefully a hasMany/belongsTo relationship can be established after both corresponding models are returned.
  6. all this needs to be done in one big promise before entering my template

I can elaborate more if this does not make sense… thanks!

Are you working with ember-data? There this king of relations is modeled as a belongTo in your case you should define your model as:

doctor:{
    doctorContact: DS.belongTo('contact', {async: true})
}
contact: {
    contact data...
}

and it should work right away…

I am using Ember-Model, which works in a similar way, but requires a little more configuring. I understand what belongsTo does functionally. The problem is the ajax to fill that “belongsTo” model also depends an a specific key in order to call the correct API.

Essentially I have a “belongsTo_?” situation.

Every Debtor record I pull has a unique “Contact ID” key/value that links it to a specific set of contact information.

I have a model called “Debtors” and a model called “Phones”. Currently “Phones” belongsTo “Debtors” but when the URL is built, it needs to also pass parameters, including that Contact ID from the debtor model, in order for the correct API to be requested.

It is an extra step that neither Ember, Ember-Data, nor Ember-Model, have built-in. I am looking for someone more experienced in Ember and in Javascript to help push me in the right direction so I can find a custom solution.