0
votes

Ive got the same HABTM (has many and belongs to many) as described at http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

In ember-data, how does one define the relationship between physician, appointment and patient?

In Rails, it is easy enough via has many through. Or, is there no need to do a HABTM association in Ember, since it is pulling/sending data out from/to an API?

Unfortunately, http://emberjs.com/guides/models/defining-models/#toc_many-to-many shows a many-to-many association with TWO models only.

1

1 Answers

1
votes

Well, at a minimum you're going to need to add a couple of belongs-to relationships on your appointment model:

App.Appointment = DS.Model.extend({
  ...
  physician: DS.belongsTo('physician'),
  patient: DS.belongsTo('patient'),
  ...
});

Thus, whenever an appointment is saved, its child relationships will be saved with it. I assume that's what you want, since that's how the db is structured in the link you posted to the Rails guide.

The rest depends heavily on how your application is structured, especially your server's JSON API. For example, if you've got a model physician and you might be able to do something like this:

var query = { physician: physician.get('id') };
this.get('store').findQuery('appointment', query).then(function (results) {
  ...
});

If you then wanted to find all of a physician's patients, you could simply return an array of the unique patients belonging to the appointments that were found. This approach is pretty straightforward and easy to reason about, but it doesn't take full advantage of Ember Data.

Alternatively, you could try defining a has-many relationship on your physician and patient models: appointments: DS.hasMany('appointment'), which has some advantages but also requires much better knowledge of Ember Data.