Updating hasMany with relationship and saving belongsTo


#1

Guys, i have this two models:

Event.js:

export default Model.extend({
  checked       : attr({ defaultValue: false }),
  isActive      : attr(),
  createdAt     : attr(),
  updatedAt     : attr(),
  start         : attr(),
  end           : attr(),
  menPrice      : attr(),
  womenPrice    : attr(),
  information   : attr(),
  meetingPoint  : attr(),
  title         : attr(),
  hiw           : attr(),
  tip           : attr(),
  bookings      : hasMany('booking', { async: true})
});

and Booking.js:

export default Model.extend({
  checked       : attr({ defaultValue: false }),
  isActive      : attr(),
  createdAt     : attr(),
  updatedAt     : attr(),
  participants  : attr(),
  email         : attr(),
  locale        : attr(),
  name          : attr(),
  observation   : attr(),
  phoneNumber   : attr(),
  event         : belongsTo('event')
});

And i would like to create the relationship referring the bookings in my events, but i just can’t figure it out how!

How it works: When i create the event in my admin dashboard, there isn’t bookings to refer, this only happens in the site, when the user makes a reservation.

I am using this code to save the booking (reservation) and refer the owner event:

let booking =  this.get('store').createRecord('booking', {
    event: this.get('event')
});

booking.save();

And is working. That’s how the booking looks like:

This is my booking JSON:

{
    "_id": {
        "$oid": "56beb58da080cf2c2d46065b"
    },
    "relationships": {
        "event": {
            "data": {
                "id": "56baa0cdd79cd63c7a0f0065",
                "type": "events"
            }
        }
    },
    "type": "bookings",
    "attributes": {
        "created-at": {
            "$date": "2016-02-13T04:48:13.489Z"
        },
        "gender": "null",
        "hostel": "null",
        "phone-number": "918918918118",
        "observation": "obs",
        "name": "Marcelo",
        "locale": "es",
        "email": "marcelo@pubcrawlsp.com",
        "participants": 10,
        "value": 0,
        "is-active": false,
        "vip": []
    },
    "__v": 0
}

As you can see, the relationship is working with the booking.

Now i need the opposite, i mean… update the event with the booking inside … but i can’t, i just can’t figure it out how! I’m stuck on this at least for 3 weeks.

I already try several things, including using the embedded option, updating manually, but nothing works.

I appreciate the help!


Update:

Nobody?


#2

Hi Marcelo, so you are trying to save (update) an event that has many bookings.

What is not working? How exactly are you attempting to solve it? Can you show the code for what you do “manually”? What is the “embedded option”, is that DS.EmbeddedRecordsMixin? Which serializers are you using?