Sails.js query for waterline association -


in sails.js i've post model many-to-many association model called "tag"

posts model

module.exports = {      attributes: {       title:        'string',       content:      'string',       coverimage:   'string',       owner: {           model: 'user'       },       tags: {           collection: 'contenttag',                  via: 'posts',             dominant: true // ---       }     }  }; 

tags model

module.exports = {    attributes: {       name: 'string',       posts: {           collection: 'post',           via: 'tags'       }   } }; 

now want related posts same tags. i've try play around .populate('tags') , and .populate('tags',{name: ['tag1','tag2']) can't figure out how solve.

share|improve question
up vote 2 down vote accepted

you can reverse query

tags.find({name:['tag1','tag2']}).populate('posts')

share|improve answer
1  
yes i've tried bus result set of tag relative posts, not set of posts. i've parse results in order consolidate.... inefficient. – ilproff_77 may 15 '15 @ 20:03
1  
inefficient yes. efficient way native / query methods. if want use waterline have tags.find({name:['tag1','tag2']}).populate('posts').exec(fun‌​ction(err,tagswposts‌​){posts = tagswposts.map(function(tag){if(tag.posts.length>0)return posts})}) however, issue being worked on , should solved next version of waterline believe. – meeker may 15 '15 @ 20:15
    
hi meeker, know feature request in waterline but, don't know when released, think i'll try implement solution. me, javascript stronger mine :) – ilproff_77 may 15 '15 @ 20:28
    
another solution create method in posts model returns posts in less efficient way, can replace code in on spot when feature released. – meeker may 15 '15 @ 20:30
    
now i've lost :) ... – ilproff_77 may 15 '15 @ 20:33

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments