How to dirty parent record when belongsTo relationship changes


Hey there. I have asked this in IRC a couple of times, but nobody seems to know. If I have a record and have defined a belongsTo relationship on it, (say a post has a belongsTo author relationship) when I change the author for that post (as in set the author relationship of the post to a different author) I would expect for the post to become dirty, but it does not. Worse, when I define an observer to transition the post to the dirty state manually when this change occurs, I get an error when committing. So I guess my question is simple: What is the recommended way to do this at the moment? (Using latest ember & ember-data rc7)


It works in ember data master. (and I don’t see any recent changes that would have fixed a bug in that area).

Is it what you are describing?

    App.Author = DS.Model.extend({
      posts: DS.hasMany('App.Post')

    App.Post = DS.Model.extend({
      author: DS.belongsTo('App.Author')

  store.load(App.Author, { id: 1});
  store.load(App.Author, { id: 2});
  store.load(App.Post, { id: 3, author_id: 1});

  var author2 = store.find(App.Author, 2),
      post = store.find(App.Post, 3);

  ok( !post.get('isDirty'), 'The post is not dirty');
  post.set('author', author2);

  ok( post.get('isDirty'), 'The post is dirty');


Hi Cyril.

Yes that is what I am describing, but the record is never set to dirty. The only difference I can see between my code and your example is that there are multiple associations defined on both models, and I am using inverse to point them at the relevant association. I’ll try and replicate with a failing test.


(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)


Ack, forgot to format the code I posted and now it won’t let me add the edited post back in! UI fail :frowning: Will post the tests I have added as soon as I can. I was looking for tests in the ember data test suite relating to this, couldn’t find any. I was looking in model_test.js and relationships_test.js am I in the wrong place?

module("DS.Model", {
  setup: function() {
    store = DS.Store.create({
      adapter: TestAdapter.create()
    Person = DS.Model.extend({
      name: DS.attr('string'),
      isDrugAddict: DS.attr('boolean')
    Post = DS.Model.extend({
      name: DS.attr('string')
      posts: DS.hasMany(Post, {inverse: 'author'})
      author: DS.belongsTo(Person, {inverse: 'posts'}) 

  teardown: function() {
    Person = null;
    Post = null;
    store = null;

test("Changing an association on a belongs to relationship should cause the parent record to become dirty", function() {
  store.load(Person, { id: 1, name: "Darth", isDrugAddict: true });
  store.load(Person, { id: 2, name: "Yoda", isDrugAddict: false });
  store.load(Post, { id: 3, name: "Invade Hoth", author_id: 2});
  var author = store.find(Person, 1),
      author_2 = store.find(Person, 2), 
      post = store.find(Post, 3);
  equal(author.get('isDirty'), false, "precond - person record should not be dirty");
  post.set('author', author);
  equal(author.get('isDirty'), true, "person record should now be dirty");

passes. I’ll keep digging.


Shouldn’t be the last line be

equal(post.get('isDirty'), true, "person record should now be dirty");