I have an issue with CompoundJS and the models relations. I try to make a relation between Creation and Photo, it works because I'm able to see "creationId" in the Photo table. But, now I'd like to get all the photos of each creation in a view I use to show all the creations. Is it possible? How am I supposed to do?
My models:
var Creation = describe('Creation', function () {
property('title', String);
property('description', Text);
property('thumbnail', Text);
property('moodboard', String);
property('date', Date);
property('location', String);
set('restPath', pathTo.creations);
});
var Photo = describe('Photo', function () {
property('name', String);
property('file', String);
set('restPath', pathTo.photos);
});
Creation.hasMany(Photo, {as: 'photos', foreignKey: 'creationId'});
Photo.belongsTo(Creation, {as: 'creation', foreignKey: 'creationId'});
My creation controller:
action(function index() {
this.title = 'Creations index';
Creation.all(function (err, creations) {
console.log(creations);
switch (params.format) {
case "json":
send({code: 200, data: creations});
break;
default:
render({
creations: creations
});
}
});
});
When I add a photo:
Photo.create(req.body.Photo, function (err, photo) {
respondTo(function (format) {
format.json(function () {
if (err) {
send({code: 500, error: photo && photo.errors || err});
} else {
send({code: 200, data: photo.toObject()});
}
});
format.html(function () {
if (err) {
flash('error', 'Photo can not be created');
render('new', {
photo: photo,
title: 'New photo',
layout: 'photos'
});
} else {
flash('info', 'Photo created');
redirect(path_to.photos);
}
});
});
});
});