0
votes

I'm working with a class which has multiple hasMany relationships of the same type, but with different attribute names. For example:

export default DS.Model.extend({
  sunday: DS.hasMany('scheduled-time'),
  monday: DS.hasMany('scheduled-time'),
  tuesday: DS.hasMany('scheduled-time'),
  wednesday: DS.hasMany('scheduled-time'),
  thursday: DS.hasMany('scheduled-time'),
  friday: DS.hasMany('scheduled-time'),
  saturday: DS.hasMany('scheduled-time'),
  ...

This might represent something like, "on Monday, employee X is scheduled to work 9am-12pm (one scheduled-time instance), and again from 1pm-5pm (another scheduled-time instance)." For each day of the week, an employee could have zero, one, or many scheduled times.

The other side of the relationship might look like this:

export default DS.Model.extend({
  start_time: DS.attr('string'),
  end_time: DS.attr('string'),
  schedule: DS.belongsTo('schedule'),
  ...

However, Ember data doesn't seem to allow this, and says that I need to explicitly state the inverse of the belongsTo (as detailed here: https://guides.emberjs.com/v2.4.0/models/relationships/). Here's where I think my data model is broken: I can't think of any logical way to say that a given scheduled-time belongs to any given day of the week. Is this data model broken? I have a feeling that instead of organizing all of the scheduled-times under individual days of the week, they should really belong collectively under a single attribute, and perhaps the days of the week would be computed properties which did some filtering instead.

Is this type of relationship possible, or should I do some refactoring?

1

1 Answers

0
votes

A shift is a scheduled-time, and an employee can hasMany shifts. The fact that shift A is on a Sunday, and shift B is also on a Sunday, but shift C is on a Tuesday doesn't make a difference.

// models/employee.js
export default DS.Model.extend({
  shifts: DS.hasMany('scheduled-time')
});