Has Many relationships in the fixture adapter

Another one. I’m trying to model a has-many relationship in fixture data:

TimeTravel.Trip.FIXTURES = [
  { id: 1, name: "Low", orders: 1, price: 100,
    start_date: "1620-09-06", end_date: "1620-11-21", hotels: []},
  { id: 2, name: "High", orders: 5, price: 100,
     start_date: "1600-09-06", end_date: "1600-09-07", hotels: [1]}
  ];

 TimeTravel.Hotel.FIXTURES = [
   { id: 1, name: "Hotel", price: 10, nights_ordered: 0, trip: 1}
 ]

But when I try to load the trip data, I get:

You looked up the 'hotels' relationship on '<TimeTravel.Trip:ember276:2>' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.attr({ async: true })

I think this worked in Ember Data pre-1.0, but I’m wondering if there’s some way to get the fixture adapter to do this correctly?

I have the exact same issue. Ember Data Version: v1.0.0-beta.1-140-ga51f29c

I had this issue. I fixed it by adding the {async: true} object into my DS.attr call. It looks like this:

App.Trip = DS.Model.extend({
  hotels: DS.hasMany('hotel', {
    async: true
  })
});

My solution for this is to manually push any fixtures into the store, bypassing the need for a hit to the adapter. Take a look at http://amalgamated.io/emberjs-and-fixtures for a simple initializer to handle this.

I’ve found it easiest to reopen the Model during setup and replace the relationship attribute.

In this example:

App.Trip = DS.Model.extend({
  hotels: DS.hasMany('hotel', {
    async: true
  })
});

gets updated in the module setup

module("Index Controller", { 
  setup: function() {
    App.Trip.reopen({
      hotels: DS.hasMany('hotel', {async: true})
    })
  },
  teardown: function() { TimeTravel.reset();
} });

By overwriting the attribute in this module it doesn’t affect the rest of the test suite.

1 Like

try using hotel_ids instead of hotels.