Javier
Javier

Reputation: 2105

Sails insert same object in two models

Is possible insert two objects equals in two models?? I have created two models Playlist and Song, and I have created a song1 and two playlists: playlist1 and playlist2; and I want that playlist1 have song1 and playlist2 have the same song (song1).

But when I relate the song1 in playlist1 and I relate song1 in playlist2, the relation that is in playlist1 dissappears. The relation is 1 to N, and I've tryed with a relation N to N, but I can't solve it.

Could anybody help me, please?? :(


Sorry, my problem is that in a controller I have the main model and work with two models.

Main model:

--- Playlist.js ---

module.exports = {
  attributes: {
     name: {
        type: 'string',
        required: true,
        unique: true
     },
     // Associations
     tablets_playlists: { 
       collection: 'Tablet',
       via: 'lista_tablet'
     },

     songs_playlist: { 
      collection: 'Song',
      via: 'song'
     }
   }
};

Other models:

--- Song.js ---

module.exports = {
  attributes: {
     group: {
        type: 'string',
        required: true,
        unique: true
     },
     song: {
        type: 'string'
     },

     // Association
     song: { 
       collection: 'Playlist',
       via: 'songs_playlist'
     }
   }
};

--- Tablet.js ---
module.exports = {
  attributes: {
     MAC: {
        type: 'string',
        required: true,
        unique: true
     },
     // Associations
     lista_tablet: { 
       collection: 'Playlist',
       via: 'tablets_playlists'
     }
};

And when I associate a song with playlist or a tablet with playlist in my render view don't appear. But if the relation is 1 to N, it appears. And when I associate two times the same song, the server crash, I think that is because there are or its trying add two equals objects in the bbdd.

This is my method to associate a tablet with a playlist and similar to song (I posted before).

associateTabletToPlaylist: function(req,res,next){
    var tablet_id = req.param('tablet_id');
    var playlist_id = req.param('playlist_id');

    Playlist.findOne(playlist_id, function foundPlaylist(err, playlist) {
        if (err) return next(err);
        if (!playlist) return next();

        playlist.tablets_playlists.add(tablet_id);
        playlist.save(function createAssociation(err, saved) {

            if (err) res.redirect('/playlist/associateTablet/');
            res.redirect('/playlist/show/' + playlist_id);
        });
    });
}

And this is the code in PlaylistController to show, to render the view:

// render the profile view (e.g. /playlist/show/.ejs)
show: function(req, res, next) {

    Playlist.findOne(req.param('id'), function foundPlaylist(err, playlist) {
        if (err) return next(err);
        if (!playlist) return next();
        Tablet.find({'listas_reproduccion':playlist.id},function foundTablets(err, tablets) {
            if (err) return next(err);
            if (!tablets) return next();
            Song.find({'song':playlist.id}, function foundSongs(err, songs) {
                if (err) return next(err);
                if (!songs) return next();
                res.view({
                    songs: songs,
                    tablets: tablets,
                    playlist: playlist
                });
            });
        });
    });
},

Upvotes: 1

Views: 86

Answers (1)

Bulkin
Bulkin

Reputation: 1040

Looks like you have one-to-one relations, but you need many-to-many.

Song have many Playlists, and Playlist have many Songs. You must give via attribute:

Songs model:

module.exports = {

    attributes: {
        name:'STRING',

        // Add a reference to Playlists
        playlists: {
            collection: 'playlists',
            via: 'songs'
        }
    }
}

Playlists model:

module.exports = {

    attributes: {
        name:'STRING',

        // Add a reference to Songs
        songs: {
            collection: 'songs',
            via: 'playlists'
        }
    }
}

Upvotes: 2

Related Questions