0

i'm developing an app with sails.js beta and mongodb. I've two models in a many-to-many association, i can successfully associate and populate instances of these models using .add() and .populate() methods. My problem is now that the .remove() method seems to do nothing.

here the models:

//Menu.js
module.exports = {
  schema : true,
  attributes: {
    name: {
        type: 'string',
        minLength: 3,
        required: true
    },
    dishes: {
        collection: 'dish',
        via: 'menus',
        dominant: true
    }
  }

};

//Dish.js
module.exports = {
schema : true,
attributes: {
    name:  {
        type: 'string',
        minLength: 3,
        required: true
    },
    description: 'string',
    menus: {
        collection: 'menu',
        via: 'dishes'
    }
  }

};

And here the controller actions...

addDishToMenu: function(req,res,next){
    Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
        if(err) return next(err);
        if(!bean) return next();
        bean.dishes.add(req.param('dish'));            
        bean.save(function(err) {
            if(err) return next(err);
            res.redirect('/main/dishes/');
        })
    })
},

removeDishFromMenu: function(req,res,next){
    Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
        if(err) return next(err);
        if(!bean) return next();
        bean.dishes.remove(req.param('dish'));
        bean.save(function(err) {
            if(err) return next(err);                
            res.redirect('/main/menu/' + req.param('menu'));
        })
    })
}

I can't figure out what i'm doing wrong. Any ideas?

Massimo Guidi
  • 150
  • 1
  • 1
  • 8
  • 1
    Hi Massimo, I recreated your issue in this [repo](https://github.com/irlnathan/sails-remove-association-issue) and posted an [issue](https://github.com/balderdashy/sails-mongo/issues/140) in sails-mongo. – JohnGalt Apr 24 '14 at 20:21
  • Hi John, thanks for your answer, so the problem is with the mongodb adapter, i'll follow the issue you posted in sails-mongo. Meanwhile, i'll try to find out a kind of workaround. – Massimo Guidi Apr 24 '14 at 21:33

1 Answers1

1

This issue has been fixed and I confirmed it with the repo I sent earlier. If you update your sails, waterline, and sails-mongo versions you should be good to go.

JohnGalt
  • 2,851
  • 2
  • 21
  • 29
  • Great! i'm sorry for the dumb question, but which is the best way to update sails, waterline and sails-mongo? thanks for your effort... – Massimo Guidi Apr 26 '14 at 17:55
  • It depends upon how stable you want stuff...probably the quickest way to get the latest greatest, you could just delete those modules from your project (npm cache clear for good measure) and then clone the master branch of each repo finally using npm install. – JohnGalt Apr 27 '14 at 02:10
  • There is a repro in Sails rc7 that uses rc5, did that happen to you again? when I upgraded from rc5-rc7 my Unit Tests failed. – user2867106 May 23 '14 at 11:02