1
votes

I've run into this problem on two separate instances now but they are both the same problem. First here are my models for one instance of the problem:

host:

Minicron.Host = DS.Model.extend({
  hostname: DS.attr('string'),
  name: DS.attr('string'),
  created_at: DS.attr('date'),

  jobs: DS.hasMany('job')
});

job:

Minicron.Job = DS.Model.extend({
  name: DS.attr('string'),
  command: DS.attr('string'),
  created_at: DS.attr('date'),

  host: DS.belongsTo('host')
});

The JSON my job model receives:

{
  "jobs":[
    {
      "id":"94e81ce07cec25451ce711fce3d96bea",
      "name":"uname -a",
      "command":"uname -a",
      "created_at":"2014-03-10T16:25:08Z",
      "host":{
        "id":8,
        "hostname":"lucid32",
        "name":"lucid32",
        "created_at":"2014-03-08T18:13:52Z"
      },
      "executions":[
        {
          "id":406,
          "job_id":"94e81ce07cec25451ce711fce3d96bea",
          "host_id":8,
          "created_at":"2014-03-09T18:15:28Z",
          "started_at":"2014-03-09T18:15:28Z",
          "finished_at":"2014-03-09T18:15:28Z",
          "exit_status":0
        }
      ]
    }
  ]
}

and the JSON by host model receives is:

{
  "hosts":[
    {
      "id":8,
      "hostname":"lucid32",
      "name":"lucid32",
      "created_at":"2014-03-08T18:13:52Z",
      "jobs":[
        {
          "id":"94e81ce07cec25451ce711fce3d96bea",
          "name":"uname -a",
          "command":"uname -a",
          "host_id":8,
          "created_at":"2014-03-10T16:25:08Z"
        }
      ]
    }
  ]
}

The problem I'm having is when my host model is loaded the job model's host relationship seems to be overwritten. I presume this is because in the JSON each job a host has has the host_id param and ember is replacing the existing data it knows about that host with basically nothing. My API is a Sinatra app using activerecord and activemodel to serialize my models as JSON so I don't really have an easy way to remove the host_id from the response.

It's also worth noting I'm using the custom serializer here http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/, I couldn't get Ember to recognise my sideloaded data without it.

I've been trying to figure this out for a few days now and I'm all out of ideas :(

Let me know if you have any questions and if it helps the full source for my app is on GitHub https://github.com/jamesrwhite/minicron/tree/master/lib/minicron/hub

Thanks!

1

1 Answers

0
votes

You could add the Active Model Serializers gem to your Sinatra app and remove host_id from the response:

class JobSerializer < ActiveModel::Serializer 

  attributes :created_at, :started_at, :finished_at, :exit_status

end