Home » Nodejs » Updating already existing event with relationship when a booking is saved in Ember

Updating already existing event with relationship when a booking is saved in Ember

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have 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 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 it’s 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": "[email protected]",
        "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 tried several things, including using the embedded option, updating manually, but nothing works.

I appreciate the help!

Answers:

Maybe I understand the question wrongly but is there a point in actually saving the event with all the bookings ?

I mean usually for a one to many relationship like this one, the bookings will have an eventId pointing to the event they are related to but the event doesn’t hold a reference to the bookings. Saving changes on an event should automatically be reflected on all the bookings referencing it.