16

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": "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 tried several things, including using the embedded option, updating manually, but nothing works.

I appreciate the help!

IGNIS
  • 440
  • 4
  • 15
  • You will have to use a callback function or promises. I recommend you read this page :) https://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/ – phenxd Feb 05 '16 at 21:15
  • why is javascript tagged in this question, do you think the solution can be acheived in javascript? – Rajshekar Reddy Feb 26 '16 at 09:38
  • obvious, i'm using node.js. –  Feb 26 '16 at 12:51
  • what do you mean by updating event with booking inside ? a change in a booking will be reflected in event ? if so , how would this happen , and you haven't created this relation ? – ProllyGeek Feb 27 '16 at 14:13
  • The exiftool tag on this question seems misplaced. – StarGeek Jul 12 '16 at 00:28
  • Maybe I understand the question wrongly but is there a point in actually saving the event with all the bookings ? – Nazim Dec 13 '16 at 16:27

2 Answers2

1

The question is quiet old but let me give an answer anyways as it's highly upvoted.

Ember Data does not serialize the has-many site of one-to-many relationships by default in update requests. This could be customized by overriding shouldSerializeHasMany method of the responsible serializer.

Please be aware that Ember Data does not serialize the many-part of one-to-many relationships by default cause that is likely to cause race conditions and conflicts. Imagine two users A and B are fetching the current relationships from backend at the same time. A adds a new related record. A little bit later B adds another related record by replacing the existing relationships with a new list adding one resource. As B is not aware of the record added by A it will be silently removed. B overrides the change of A.

JSON:API specification supports relationship links to address these issue even for many-to-many and many-to-none relationships. But for many-to-one only updating one-side is the easiest solution.

jelhan
  • 6,149
  • 1
  • 19
  • 35
0

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.

Nazim
  • 367
  • 2
  • 11