home
  • Blog
5.11
  • Introduction
  • Getting Started
  • Tutorial
  • Core Concepts
  • Components
  • Routing
  • Services
  • EmberData
    • Introduction
    • Defining Models
    • Finding Records
    • Creating, Updating and Deleting
    • Relationships
    • Pushing Records into the Store
    • Handling Metadata
    • Customizing Adapters
    • Customizing Serializers
  • In-Depth Topics
  • Application Development
  • Application Concerns
  • Accessibility
  • Configuration
  • Testing
  • Addons and Dependencies
  • Using TypeScript
  • Developer Tools
  • Ember Inspector
  • Code Editors
  • Additional Resources
  • Upgrading
  • Contributing to Ember.js
  • Glossary
Old Guides - You are viewing the guides for Ember v5.11.0.
Go to v6.3.0

Creating, Updating and Deleting


Creating Records

You can create records by calling the createRecord() method on the store.

this.store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

To make store available, you can inject the store service.

Updating Records

Making changes to EmberData records is as simple as setting the attribute you want to change:

this.store.findRecord('post', 1).then(function(post) {
  // ...after the record has loaded
  post.title = 'A new post';
});

Persisting Records

Records in EmberData are persisted on a per-instance basis. Call save() on any instance of Model and it will make a network request.

EmberData takes care of tracking the state of each record for you. This allows EmberData to treat newly created records differently from existing records when saving.

By default, EmberData will POST newly created records to their type URL.

let post = store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

post.save(); // => POST to '/posts'

Records that already exist on the backend are updated using the HTTP PATCH verb.

store.findRecord('post', 1).then(function(post) {
  post.title; // => "Rails is Omakase"

  post.title = 'A new post';

  post.save(); // => PATCH to '/posts/1'
});

You can tell if a record has outstanding changes that have not yet been saved by checking its hasDirtyAttributes property. You can also see which parts of the record were changed and what the original value was using the changedAttributes() method. changedAttributes returns an object, whose keys are the changed properties and values are an array of values [oldValue, newValue].

person.isAdmin; // => false
person.hasDirtyAttributes; // => false
person.isAdmin = true;
person.hasDirtyAttributes; // => true
person.changedAttributes(); // => { isAdmin: [false, true] }

At this point, you can either persist your changes via save() or you can roll back your changes using rollbackAttributes().

person.hasDirtyAttributes; // => true
person.changedAttributes(); // => { isAdmin: [false, true] }

person.rollbackAttributes();

person.hasDirtyAttributes; // => false
person.isAdmin; // => false
person.changedAttributes(); // => {}

Handling Validation Errors

If the backend server returns validation errors after trying to save, they will be available on the errors property of your model. Here's how you might display the errors from saving a blog post in your template:

{{#each this.post.errors.title as |error|}}
  <div class="error">{{error.message}}</div>
{{/each}}
{{#each this.post.errors.body as |error|}}
  <div class="error">{{error.message}}</div>
{{/each}}

Promises

save() returns a promise, which makes it easy to asynchronously handle success and failure scenarios. Here's a common pattern:

// Assumed to have already injected the router and store services
const newPost = this.store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

try {
  await newPost.save();
  this.router.transitionTo('posts.show', newPost.id);
} catch (error) {
  // Handle error
}

Deleting Records

Deleting records is as straightforward as creating records. Call deleteRecord() on any instance of Model. This flags the record as isDeleted. The deletion can then be persisted using save(). Alternatively, you can use the destroyRecord method to delete and persist at the same time.

let post = store.peekRecord('post', 1);
post.deleteRecord();
post.isDeleted; // => true
post.save(); // => DELETE to /posts/1

// OR
post = store.peekRecord('post', 2);
post.destroyRecord(); // => DELETE to /posts/2
left arrow
Finding Records
Relationships
right arrow
On this page

  • Creating Records
  • Updating Records
  • Persisting Records
  • Handling Validation Errors
  • Promises
  • Deleting Records
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2025 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue Created with Sketch.