{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/926","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/926/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/926/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/926/events","html_url":"https://github.com/tgriesser/bookshelf/issues/926","id":108035081,"number":926,"title":"Filtering a join","user":{"login":"bsiddiqui","id":2721959,"avatar_url":"https://avatars.githubusercontent.com/u/2721959?v=3","gravatar_id":"","url":"https://api.github.com/users/bsiddiqui","html_url":"https://github.com/bsiddiqui","followers_url":"https://api.github.com/users/bsiddiqui/followers","following_url":"https://api.github.com/users/bsiddiqui/following{/other_user}","gists_url":"https://api.github.com/users/bsiddiqui/gists{/gist_id}","starred_url":"https://api.github.com/users/bsiddiqui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsiddiqui/subscriptions","organizations_url":"https://api.github.com/users/bsiddiqui/orgs","repos_url":"https://api.github.com/users/bsiddiqui/repos","events_url":"https://api.github.com/users/bsiddiqui/events{/privacy}","received_events_url":"https://api.github.com/users/bsiddiqui/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tgriesser/bookshelf/labels/question","name":"question","color":"cc317c"},{"url":"https://api.github.com/repos/tgriesser/bookshelf/labels/relations","name":"relations","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-24T00:49:57Z","updated_at":"2015-09-24T03:46:07Z","closed_at":null,"body":"I have a few tables `users`, `orders`, `orders_users`, and `addresses`\r\n\r\nThe `OrdersUser` model joins `User`, `Order`, and `Address`\r\n\r\nOn my `Address` model I have the following relations\r\n\r\n```js \r\norder: function () {\r\n   this.belongsToMany('Order').through('OrdersUser')\r\n}\r\n```\r\n\r\nIs there are a way for me to query this relationship to filter by a given user? That way I can get all the orders for a given user and address?\r\n\r\nI wanted to do something like this: \r\n```js\r\nreturn addressModel.forge({ id: 1 }, { withRelated: {\r\n   'order': function (qb) {\r\n       return qb.where({ user_id: 1 })\r\n   }\r\n}}).fetch()\r\n```\r\nBut that queries `Order` for the given attributes and I need to query `OrdersUser`\r\n"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/142800095","html_url":"https://github.com/tgriesser/bookshelf/issues/926#issuecomment-142800095","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/926","id":142800095,"user":{"login":"rhys-vdw","id":822105,"avatar_url":"https://avatars.githubusercontent.com/u/822105?v=3","gravatar_id":"","url":"https://api.github.com/users/rhys-vdw","html_url":"https://github.com/rhys-vdw","followers_url":"https://api.github.com/users/rhys-vdw/followers","following_url":"https://api.github.com/users/rhys-vdw/following{/other_user}","gists_url":"https://api.github.com/users/rhys-vdw/gists{/gist_id}","starred_url":"https://api.github.com/users/rhys-vdw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rhys-vdw/subscriptions","organizations_url":"https://api.github.com/users/rhys-vdw/orgs","repos_url":"https://api.github.com/users/rhys-vdw/repos","events_url":"https://api.github.com/users/rhys-vdw/events{/privacy}","received_events_url":"https://api.github.com/users/rhys-vdw/received_events","type":"User","site_admin":false},"created_at":"2015-09-24T03:46:07Z","updated_at":"2015-09-24T03:46:07Z","body":"Add `debug: true` to `options` argument to `fetch`. If you're joining on `user_id` there should be no difference between `orders.user_id` and `orders_users.user_id`. Either way the solution should be:\r\n\r\n```js\r\n  withRelated: {\r\n    'order': function (qb) { return qb.where({ 'orders_users.user_id': 1 }) }\r\n}\r\n```"}}