angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

belongsTo relation

Open kalko91 opened this issue 9 years ago • 1 comments

HI guys. I have a problem. help me please.

I have API url example this: PUT /api/mcus/{id}/trailer/{trailerId} Assign MCU device to Trailer

Trailer Model

var Trailer = restmod.model('api/trailers');
        Trailer.mix({
            mcu: { hasOne: 'Mcu'}
        });

Mcu Model

var Mcu = restmod.model('api/mcus');
        Mcu.mix({
            trailer: { belongsTo: 'Trailer'},
        });

Update Mcu model

vm.model = Mcu.$find(1).$then(function(){
                    var trailer = TrailerModel.$find(3).$then(function(){
                         vm.model.trailer = trailer;
                        vm.model.$save(); // url PUT /api/mcus/1
                        vm.model.trailer.$save() // url PUT /api/trailers/3
                    })

In mcu model i have this json

{
   "id":1,
   "deviceId":5336091833,
   "serialNo":"test",
   "ipAddress":"127.0.0.1",
   "trailer":{
      "id":3,
      "title":"Test Trailer",
      "description":"test"
   },
   "sensors":[

   ]
}

Whot I have to do to get this link? // PUT /api/mcus/{id}/trailer/{trailerId}

Thanks!

kalko91 avatar Sep 01 '16 13:09 kalko91

You don't want to be mixing belongsTo and hasOne. Those are two different styles of relations. You would want to define it like the following:

var Trailer = restmod.model();  // Note that we're not passing a route 

var Mcu = restmod.model('api/mcus');
        Mcu.mix({
            trailer: { hasOne: Trailer},
        });

jpulec avatar Sep 22 '16 01:09 jpulec