mardi 8 décembre 2015

How to serialize embedded belongsTo relationship

I'm using the master ember-cli-mirage branch to take advantage of active model serializer and relationships support, now i've a fully working example with hasMany relationships, but now given a model User that hasOne Organization i can't figure out how to embed the organization json into the user json, so far now i'be been trying to use belongsTo with no luck as it tries to add the foreign key to the user, looks like if the user has no organization it throws an error because it validates if the resource (null as there is no organization) is a model and if it's not then it tries to map as a collection resulting in the error.

Not sure how to achieve this behavior, i have tricked the user model by setting the user as if it belongs to the organization (it's indeed the opposite) and assigning the organization id to the user organizationId, this way it embeds the organization object into the user json, but it's obviously just a hack and not the right/expected way to do this having in account hasOne is a common pattern.

Any advise or suggestion is appreciated.




Aucun commentaire:

Enregistrer un commentaire