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

Cannot assign value to hasOne

Open MickCrozier opened this issue 10 years ago • 2 comments

Attempting to $save() with the proper model object or just the id in the hasOne attribute never gets posted.

.factory('Contact', ['restmod', function(restmod){
        var model = restmod.model('api/contact').mix({

        });

        return model;
    }])
.factory('Staff', ['restmod', function(restmod){
        var model = restmod.model('api/staff').mix({
            contact: { hasOne: 'Contact' },
        });

        return model;
    }])
newStaff = $build({contact:myContact});
newStaff.$save();

Whether I use the Contact restmod object or just the id - contact is never posted.

Cheers!

MickCrozier avatar Feb 18 '15 01:02 MickCrozier

If you need to send the Contact id when the Staff record is saved then you should use a belongsTo association instead.

iobaixas avatar Feb 18 '15 13:02 iobaixas

Cheers @iobaixas I have the Contact object belongsTo is not working as expected - although I could be missing something. In fact if I remove the hasOne relationship - it doesn't seem to matter if the staff: {belongsTo:'Contact', key:'contact'} is present or not.

While the Staff create/post works Update/put throws me an error on the server as it's expecting contact as an id, not an object Also causes issues when I get Staff - with hasOne relationship the child Contact is setup as restmod object

The logic is: a Staff has one Contact. There is also many others people types that have one Contact.

MickCrozier avatar Feb 18 '15 23:02 MickCrozier