pushPayload and slugs cause duplicate records :(

We’re using websockets in our app to provide users with realtime updates. We’re pushing the incoming data in the store using pushPayload. We’re also using slugs based on for instance a title, instead of integer id’s, however, this causes duplicate objects to show up once someone changes one of those titles.

I could send an extra uid attribute or something and then search the store and delete the old record, but that seems nasty.

Has anyone else ran in to this? How did you solve it?

1 Like

It seems that even without using websockets this is going to be a problem in ember-data beta14 since it does not allow for the id of objects to change, when this is a slug, it will, however.

How are people dealing with this? I really hate just exposing db id’s to the client because it is so meaningless to users…

Maybe it’s a bit of a hack, but perhaps an idea would be to create an ApplicationSerializer and implement the normalizePayload() hook to filter the incoming payload.

That way you could do some kind of cached mapping which insures that the original id is kept intact.

I believe that EmberData currently assumes that ids are not going to change. pushPayload uses store._load method under the hood which looks for a record with a given id and then updates it.

So, it’s currently not possible to use slugs then? (assuming we would need the slugs to update)

It’s possible to use slugs, but you also need to have a unique identifier, which doesn’t change. For example in Travis CI when you go to /emberjs/ember.js path, the app will fetch repository with an emberjs/ember.js slug, but the downloaded record will have also regular DB id.

In other words: what you use in an URL doesn’t need to be the same thing that you use as a primary key.

1 Like