Update other records in the servers response to a records `save()` call


#1

I have a model that, when created or updated on the server, will sometimes modify other records of the same model. When it changes the values of other records I need to send those changes to the client. I’ve tried including the changed records in sideloaded objects but Ember raised this error Assertion Failed: Your server returned a hash with the key queued_episodes but you have no mapping for it. What do I need to handle the extra updated records?

Each queued_episode is a record in a reorderable list stored in SQL. The list uses a bisection insertion scheme. Each episode has an index between MIN_INT and MAX_INT. When an episode is added to the queue its index is set to the midpoint between the surrounding episodes.

MIN_INT = 0
MAX_INT = 16

queue.push episode1
=> idx: 8
queue.push episode2
=> idx: 12
queue.push episode3
=> idx: 14

queue.insert episode4, after: episode2
=> idx: 13

queue.all
=> [
episode: episode1, idx: 8,
episode: episode2, idx: 12,
episode: episode4, idx: 13,
episode: episode3, idx: 14
]

I’m pretty happy with this data structure, it allows quick insertions, deletions, and reordering (If there is a better way to store reorderable lists in SQL I’d like to know).

The problem, as you can see in the example above, is that indexes can converge, and there may not be space to insert another record between two other records. In the example above you couldn’t insert a record between episodes 2 and 4, or 4 and 3. I’ve fixed this in the data structure by checking if the indexes are within one of each other, then recalculating indexes, distributing them evenly throughout the address space.

queue.insert episode5, after: episode2
queue.rebased?
=> true

queue.all
=> [
episode: episode1, idx: 3,
episode: episode2, idx: 6,
episode: episode5, idx: 9
episode: episode4, idx: 12,
episode: episode3, idx: 15
]

This logic is server side, if the ember app had inserted episode5 after episode2 it would have received the json of episode5 with the rebased index of 9, but it hasn’t updated the indexes for the other episodes, so episode5 would be placed between episodes 1 and 2.

I tried returning an array of queued_episodes in addition to the singular queued_episode, but Ember raises this error Error: Assertion Failed: Your server returned a hash with the key queued_episodes but you have no mapping for it.