# Deleting model from store after save failure

**URL:** <https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072>\
**Category:** Ember Data\
**Created:** [April 29, 2014, 2:29pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072 "2014-04-29T14:29:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiloginkel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thiloginkel/32/5686_2.png) [@thiloginkel](https://discuss.emberjs.com/u/thiloginkel)\
**Post date:** [April 29, 2014, 2:29pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/1 "2014-04-29T14:29:24Z")

</div>

I am using the `ActiveModelAdapter` to store models in our Rails backend. When the save fails, I’d like to remove it from the store. I tried verious different variantes, but none worked so far.

My base code snippet looks like this:

```
  c = @store.createRecord('commission',
    {
      amount: -parseFloat(App.normalizeAmount(@get('withdrawalAmount'))),
      commissionType: 'withdrawal'
    }
  )
  c.save().then(->
    Us.showSuccess(App.t('statement.withdrawal.booked'))
  ).catch(->
    Us.showError(App.t('statement.withdrawal.booking_failed'))
    c.deleteRecord()
  )

```

Unfortunately, `c.deleteRecord()` raises an assertion error:

```
Assertion Failed: Error: Attempted to handle event `deleteRecord` on <App.Commission:ember1008:null> while in state root.loaded.created.inFlight.  

```

Instead of `c.deleteRecord()` I tried `@store.deleteRecord(c)`, which raises the same assertion error. I also tried `c.rollback()`, which has no effect.

Any ideas how to get rid of the erratic record?

```
Ember Data : 1.0.0-beta.7+canary.ab589b32 

```

---

<div class="post-metadata">

**Author:** ![midd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/midd/32/196_2.png) [@midd](https://discuss.emberjs.com/u/midd)\
**Post date:** [April 29, 2014, 3:36pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/2 "2014-04-29T15:36:58Z")

</div>

try to use rollback()

```
c.save().then(->
    Us.showSuccess(App.t('statement.withdrawal.booked'))
  ).catch(->
    Us.showError(App.t('statement.withdrawal.booking_failed'))
    c.rollback();
  )

```

---

<div class="post-metadata">

**Author:** ![thiloginkel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thiloginkel/32/5686_2.png) [@thiloginkel](https://discuss.emberjs.com/u/thiloginkel)\
**Post date:** [April 29, 2014, 3:49pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/3 "2014-04-29T15:49:36Z")

</div>

I tried calling `rollback()`, but the record is still in the store afterwards.

---

<div class="post-metadata">

**Author:** ![alanhecht](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alanhecht/32/16158_2.png) [@alanhecht](https://discuss.emberjs.com/u/alanhecht)\
**Post date:** [April 30, 2014, 2:16pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/4 "2014-04-30T14:16:33Z")

</div>

You need to call unloadRecord() after calling rollback(). It is a two step process in that you can’t unload dirty records from the store nor can you unload records that are in the process of being deleted (otherwise known as being ‘inflight’).

* * *

---

<div class="post-metadata">

**Author:** ![thiloginkel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thiloginkel/32/5686_2.png) [@thiloginkel](https://discuss.emberjs.com/u/thiloginkel)\
**Post date:** [April 30, 2014, 5:17pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/5 "2014-04-30T17:17:31Z")

</div>

Unfortunately, that also yields an assertion error:

```
Assertion Failed: Error: Assertion Failed: You can only unload a record which is not inFlight. `<App.Commission:ember825:null> ` 

```

Code:

```
  c = @store.createRecord('commission',
    {
      amount: -parseFloat(App.normalizeAmount(@get('withdrawalAmount'))),
      commissionType: 'withdrawal'
    }
  )
  c.save().then(->
    Us.showSuccess(App.t('statement.withdrawal.booked'))
  ).catch(->
    Us.showError(App.t('statement.withdrawal.booking_failed'))
    c.rollback()
    c.unloadRecord()
  )

```

---

<div class="post-metadata">

**Author:** ![alanhecht](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alanhecht/32/16158_2.png) [@alanhecht](https://discuss.emberjs.com/u/alanhecht)\
**Post date:** [May 1, 2014, 12:37am UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/6 "2014-05-01T00:37:13Z")

</div>

Looks like you got back a 422 response, which right now leaves the record in an ‘inFlight’ state (see [https://github.com/emberjs/data/issues/1632](https://github.com/emberjs/data/issues/1632) and [https://github.com/emberjs/data/issues/1764](https://github.com/emberjs/data/issues/1764)). My workaround when getting a 422 response was to call record.transitionTo(‘created.uncommitted’). From there you should be able to delete the record. I’m guessing this will change in a future release of Ember Data since there’s been some discussion around it.

---

<div class="post-metadata">

**Author:** ![thiloginkel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thiloginkel/32/5686_2.png) [@thiloginkel](https://discuss.emberjs.com/u/thiloginkel)\
**Post date:** [May 2, 2014, 9:01am UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/7 "2014-05-02T09:01:57Z")

</div>

Thanks, that worked. It only works for 422 status codes, though. For 400, no workaround is necessary and applying the `transitionTo('created.uncommitted')` workaround raises another error. _sigh_

Any recommendations for a robust solution? For the time being, I’ll resort to just returning a 400 status from the backend…

---

<div class="post-metadata">

**Author:** ![alanhecht](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alanhecht/32/16158_2.png) [@alanhecht](https://discuss.emberjs.com/u/alanhecht)\
**Post date:** [May 6, 2014, 12:01am UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/8 "2014-05-06T00:01:39Z")

</div>

Unfortunately, I don’t. I’m somewhat new to Ember Data myself. I was hoping somebody more knowledgable and experienced would weigh in with an opinion. From what I saw in this - [http://reefpoints.dockyard.com/2014/04/29/the-problem-with-server-rendered-errors.html](http://reefpoints.dockyard.com/2014/04/29/the-problem-with-server-rendered-errors.html) - handling server side errors can be a thorny problem to generalize.

Update: I would read [Ember Data, ActiveModelAdapter, and Validations](http://discuss.emberjs.com/t/ember-data-activemodeladapter-and-validations/4804) for further discussion on the topic.

---

<div class="post-metadata">

**Author:** ![jens](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jens/32/15494_2.png) [@jens](https://discuss.emberjs.com/u/jens)\
**Post date:** [August 18, 2014, 5:58pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/9 "2014-08-18T17:58:52Z")

</div>

hi @thiloginkel, i ran into the same kind of frustration today. The following workarround works for me at the moment:

```
    var model = this.get('model');
    var store = model.store;

    model.deleteRecord();
    model.save().catch(function(err){
      model.transitionTo('loaded.saved');
      var payload = model.serialize({includeId: true});
      store.unloadRecord(model)
      store.pushPayload('nestedSet',{nested_set:payload});
    });

```

Happy if anyone has a better solution for this 😃

---

<div class="post-metadata">

**Author:** ![DevinRhode2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/devinrhode2/32/14826_2.png) [@DevinRhode2](https://discuss.emberjs.com/u/DevinRhode2)\
**Post date:** [March 2, 2018, 7:40pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/10 "2018-03-02T19:40:57Z")

</div>

> [@jens](#):
>
> model.transitionTo(‘loaded.saved’); var payload = model.serialize({includeId: true}); store.unloadRecord(model) store.pushPayload(‘nestedSet’,{nested\_set:payload});

Worked for me! Thanks @jens!

---

<div class="post-metadata">

**Author:** ![DevinRhode2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/devinrhode2/32/14826_2.png) [@DevinRhode2](https://discuss.emberjs.com/u/DevinRhode2)\
**Post date:** [March 2, 2018, 7:46pm UTC](https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/11 "2018-03-02T19:46:40Z")

</div>

I actually only needed to add the loaded.saved line:

```
  record.destroyRecord().finally(() => {
    // this bug should be fixed in future version of ember data (https://github.com/emberjs/data/issues/1632)
    // fix from https://discuss.emberjs.com/t/deleting-model-from-store-after-save-failure/5072/8
    record.transitionTo('loaded.saved');
    record.unloadRecord();
  });

```

I’m using ember 2.13
