mercredi 12 juin 2019

Ember Serializer works for either the model or the model through relationship, but not both

I have a Rails API connecting to Ember

I have a many-to-many relationship with Doctor has_many patients and Patient has_many doctors .

**Here is my doctor model

export default Model.extend({
  // Attributes
  name: attr('string'),
  address: attr('string'),
  patients: hasMany(‘patient')
});

**Here is my patient model

export default Model.extend({
  // Attributes
  name: attr('string'),
  issue: attr(‘string’),
  age: attr(’number’),
  visits: attr(‘number’),
  doctors: hasMany(‘doctor’)
});

The Doctor data displays perfectly when calling it.

Here is my issue based on the patient serializer

export default JSONAPISerializer.extend({

});

If I leave it blank like above Doctor.patients data displays great, but if I try to display all patients without Doctor it shows any attr number as undefined.

If I add a method to decamilze like this

export default JSONAPISerializer.extend({
     keyForAttribute: function(key) {
      return decamelize(key);
    },
});

When adding the decimalize - showing all patients data by themselves now works great, but Doctor.patients data does not display. Instead the number attr are now undefined.

Is there a way to only use decamilize for patients and not for the relationship through Doctor?




Aucun commentaire:

Enregistrer un commentaire