# Why does store.createRecord create a record immediately?

**URL:** <https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161>\
**Category:** Ember Data\
**Created:** [October 29, 2013, 12:28pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161 "2013-10-29T12:28:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![denzo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/denzo/32/15182_2.png) [@denzo](https://discuss.emberjs.com/u/denzo)\
**Post date:** [October 29, 2013, 12:28pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/1 "2013-10-29T12:28:07Z")

</div>

Let’s say I have a list of items displayed using store.all(‘product’)

When I click add product button in my method I use. store.createRecord(‘product’, {name: ‘Awesome Product’})

The new product appears in the list immediately, even though it is not stored on the server.

store.createRocord returns a newly created record which I have to call save() on to persist it on the server.

What is the right way to create new records so they only appear in the store after they are saved?

I would think that store.push method would be more appropriate for adding records to the store without storing it on the server.

---

<div class="post-metadata">

**Author:** ![ashraf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ashraf/32/13153_2.png) [@ashraf](https://discuss.emberjs.com/u/ashraf)\
**Post date:** [October 29, 2013, 12:50pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/2 "2013-10-29T12:50:36Z")

</div>

I believe, at a high level, the reason is to improve responsiveness but I think also to allow for client-side validation, if applicable in your model (I might be wrong with the second point - that’s my own understanding). You can indicate that a record is stored locally with a watched property that your update once your Adapter saves the record by hooking into the appropriate record lifecycle event handlers (didCreate or didUpdate) and update the property there or use `product.get("stateManager.currentState.name")` to find where your save process is. Use this property to add some CSS and watch it until it changes and change your CSS accordingly.

The following article gives a good [introduction to records and ember data](http://darthdeus.github.io/blog/2013/01/27/ember-data-in-depth)

---

<div class="post-metadata">

**Author:** ![MiguelMadero](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/miguelmadero/32/15187_2.png) [@MiguelMadero](https://discuss.emberjs.com/u/MiguelMadero)\
**Post date:** [November 2, 2013, 3:59pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/3 "2013-11-02T15:59:47Z")

</div>

I think what you want is `buildRecord` instead of `createRecord`. The latter sets that as loaded, which means is on the store, so it will appear on lists that might be bound to that collection. Hope this helps.

---

<div class="post-metadata">

**Author:** ![lookingsideways](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lookingsideways/32/13652_2.png) [@lookingsideways](https://discuss.emberjs.com/u/lookingsideways)\
**Post date:** [November 3, 2013, 1:36pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/4 "2013-11-03T13:36:09Z")

</div>

`buildRecord` is marked as private in the API docs, does that mean it’s not meant to be part of the public API?

It definitely sounds useful, are there any gotchas to look out for when using it?

---

<div class="post-metadata">

**Author:** ![superlou](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/superlou/32/14585_2.png) [@superlou](https://discuss.emberjs.com/u/superlou)\
**Post date:** [November 4, 2013, 8:26pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/5 "2013-11-04T20:26:43Z")

</div>

Also interested with @lookingsideways on if there are any unexpected consequences on using `buildRecord`.

---

<div class="post-metadata">

**Author:** ![Emerson](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/emerson/32/14405_2.png) [@Emerson](https://discuss.emberjs.com/u/Emerson)\
**Post date:** [November 26, 2013, 4:59pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/6 "2013-11-26T16:59:16Z")

</div>

We also ran into this problem as well. Coming from Rails, it feels like buildRecord should be a public API method. It’s annoying to have to clean up invalid/partial records that get dumped into the store.

---

<div class="post-metadata">

**Author:** ![rzurad](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rzurad/32/14877_2.png) [@rzurad](https://discuss.emberjs.com/u/rzurad)\
**Post date:** [November 27, 2013, 6:20pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/7 "2013-11-27T18:20:12Z")

</div>

It is my understanding that the this is all by design. Internally, there is a RecordArrayManager that watches and responds to record changes in the store (adding a record, deleting a record, changing a record), and updates all RecordArrays that care about the model that has changed.

There is a RecordArray and a FilteredRecordArray (and an AdapterPopulatedRecordArray, but that’s not important here), but it is important to note that the RecordArray returned by `store.all`, while it is **not** a FilteredRecordArray instance, it is technically filtered in that it is registered with the RecordArrayManager. Its filter is very simple: it shall contain every record in the store that is of this particular type, regardless of anything else about it–state, isDirty, isNew, isDeleted, etc. So when you display a list backed by the RecordArray returned by `store.all`, it makes perfect sense that it immediately shows records that were just created via `createRecord`.

What you actually want to do instead of `store.all` is `store.filter` and set your filter only returns records of that type that are not `isNew`.

`buildRecord` is private because the client should never be creating records without receiving them from the persistence layer. `createRecord` creates records marked as dirty and new so that there can be a future point of synchronization with your persistence layer. If you need a record that is not dirty, you must retrieve it from the persistence layer (or some other trusted single source of truth, such as a local cache). Similarly, you shouldn’t use `store.push` because that method expects the deserialized POJOs that are about to be pushed into the store to be of a very specific format (the responsibility of translating the format of the response from your persistence layer into the format that `store.push` wants is left to your Adapter and your Serializer)

So while there is nothing saying that you can’t use `buildRecord`, what Ember-Data wants you do to is use createRecord when you actually are creating a brand new record that does not exist in the persistence layer, and use `store.filter` to make your list only show what you want it to show.

EDIT: I was incorrect when I said “it shall contain every record in the store that is of this particular type, regardless of anything else about it”. The only thing it cares about is whether or not the record isDeleted or isEmpty. Anything else will appear in your `all` RecordArray:

[https://github.com/emberjs/data/blob/v1.0.0-beta.3/packages/ember-data/lib/system/record\_array\_manager.js#L139](https://github.com/emberjs/data/blob/v1.0.0-beta.3/packages/ember-data/lib/system/record_array_manager.js#L139)

---

<div class="post-metadata">

**Author:** ![stefek99](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/stefek99/32/10981_2.png) [@stefek99](https://discuss.emberjs.com/u/stefek99)\
**Post date:** [June 19, 2015, 11:51am UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/9 "2015-06-19T11:51:19Z")

</div>

> [@rzurad](#):
>
> What you actually want to do instead of store.all is store.filter and set your filter only returns records of that type that are not isNew.

I like your idea but it doesn’t work for me:

```
App.IndexRoute = Ember.Route.extend({

  model: function() {
    return this.store.filter('color', function(color) {
      return color.get("isNew") === false;
    })
  }

});  

```

I need to resolve to a **hack** :

```
App.IndexRoute = Ember.Route.extend({

  setupController: function(controller) {
      var _this = this;
      return this.get('store').find('color').then(function() {
          controller.set('model', _this.get('store').filter('color', function(color){
              return color.get("isNew") === false;
          }));
      });
  }

});  

```

**hack** was found here - [Ember Data DS.Store.filter always returns empty list · Issue #1872 · emberjs/data · GitHub](https://github.com/emberjs/data/issues/1872#issuecomment-63253265) - and I’ll post complete examples / test cases in that github issue…

_(as it still seems unresolved / affecting other users)_

---

<div class="post-metadata">

**Author:** ![erichonkanen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/erichonkanen/32/13449_2.png) [@erichonkanen](https://discuss.emberjs.com/u/erichonkanen)\
**Post date:** [July 31, 2015, 11:39pm UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/10 "2015-07-31T23:39:18Z")

</div>

Hello, has anyone had a better answer to this question as I just ran into this issue now with latest ember/cli where I have a record that is briefly created and then removed if the backend save fails… wondering how to prevent this?!

EDIT:

In case anyone wondering, using store.filter resolved the issue…

```
@store.filter 'content', {}, (content)->
  content.get('isNew') is false

```

---

<div class="post-metadata">

**Author:** ![robclancy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/robclancy/32/11146_2.png) [@robclancy](https://discuss.emberjs.com/u/robclancy)\
**Post date:** [October 7, 2015, 9:15am UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/11 "2015-10-07T09:15:09Z")

</div>

One of various reasons I have decided to completely abandon ember-data. Server has my data and I want people to see what is on the server not what you have input instantly in a second area. Then having to work around ember’s link-to taking the model object so you don’t get updated data without stupid workarounds.

Websites rely on a database 99.99% of the time, just get it.

---

<div class="post-metadata">

**Author:** ![elithrar](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/elithrar/32/11352_2.png) [@elithrar](https://discuss.emberjs.com/u/elithrar)\
**Post date:** [October 7, 2015, 9:43am UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/12 "2015-10-07T09:43:10Z")

</div>

> [@robclancy](#):
>
> Server has my data and I want people to see what is on the server not what you have input instantly in a second area.

Calling `reload()` on the model after the Promise returns is the standard way of doing this in Ember Data. Save to store, call reload, then transition to the newly saved record.

---

<div class="post-metadata">

**Author:** ![jasonmit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jasonmit/32/182_2.png) [@jasonmit](https://discuss.emberjs.com/u/jasonmit)\
**Post date:** [October 12, 2015, 4:54am UTC](https://discuss.emberjs.com/t/why-does-store-createrecord-create-a-record-immediately/3161/14 "2015-10-12T04:54:28Z")

</div>

> [@robclancy](#):
>
> Server has my data and I want people to see what is on the server not what you have input instantly in a second area.

[https://vimeo.com/141974064](https://vimeo.com/141974064)
