Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

belongsTo relation #389

Open
kalko91 opened this issue Sep 1, 2016 · 1 comment
Open

belongsTo relation #389

kalko91 opened this issue Sep 1, 2016 · 1 comment
Labels

Comments

@kalko91
Copy link

kalko91 commented Sep 1, 2016

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!

@jpulec
Copy link
Contributor

jpulec commented Sep 22, 2016

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},
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants