{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/578","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/578/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/578/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/578/events","html_url":"https://github.com/tgriesser/bookshelf/issues/578","id":54675417,"number":578,"title":"through-model 'saving' listener not firing on .attach()","user":{"login":"ihinsdale","id":4626169,"avatar_url":"https://avatars.githubusercontent.com/u/4626169?v=3","gravatar_id":"","url":"https://api.github.com/users/ihinsdale","html_url":"https://github.com/ihinsdale","followers_url":"https://api.github.com/users/ihinsdale/followers","following_url":"https://api.github.com/users/ihinsdale/following{/other_user}","gists_url":"https://api.github.com/users/ihinsdale/gists{/gist_id}","starred_url":"https://api.github.com/users/ihinsdale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ihinsdale/subscriptions","organizations_url":"https://api.github.com/users/ihinsdale/orgs","repos_url":"https://api.github.com/users/ihinsdale/repos","events_url":"https://api.github.com/users/ihinsdale/events{/privacy}","received_events_url":"https://api.github.com/users/ihinsdale/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tgriesser/bookshelf/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-17T21:12:26Z","updated_at":"2015-01-17T22:35:13Z","closed_at":null,"body":"I have models A, B, and C, where C is a through-model between A and B:\r\n```\r\nvar A = bookshelf.model.extend({\r\n  table: 'as',\r\n  ...\r\n  bRelation: function() {\r\n    return this.belongsToMany('B').through('C');\r\n  }\r\n  ...\r\n});\r\n\r\nvar B = bookshelf.model.extend({\r\n  table: 'bs',\r\n  ...\r\n  aRelation: function() {\r\n    return this.hasMany('A').through('C');\r\n  }\r\n  ...\r\n});\r\n\r\nvar C = bookshelf.model.extend({\r\n  table: 'as_joined_to_bs',\r\n  ...\r\n  initialize: function() {\r\n    this.on('saving', this.doSomething);\r\n  },\r\n  aRelation: function() {\r\n    return this.belongsTo('A');\r\n  },\r\n  bRelation: function() {\r\n    return this.belongsTo('B');\r\n  },\r\n  doSomething: function() {\r\n    console.log('did something');\r\n  }\r\n  ...\r\n});\r\n```\r\nWhen I try to insert into the `as_joined_to_bs` join table via:\r\n```\r\ninstanceOfA.bRelation().attach([ instanceOfB ], { transacting: t })\r\n```\r\nthe insert succeeds, but `doSomething` never gets called. From what I can tell this is because Bookshelf's [pivotHelpers._processPivot()](https://github.com/tgriesser/bookshelf/blob/master/lib/relation.js#L432) inserts into the join table directly with a Knex query, rather than by trying to save an instance of through-model C. Is that correct? Is there any existing Bookshelf-ic way to make `doSomething` get called on `.attach()` (before the row has been inserted)?\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/70387510","html_url":"https://github.com/tgriesser/bookshelf/issues/578#issuecomment-70387510","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/578","id":70387510,"user":{"login":"bendrucker","id":808808,"avatar_url":"https://avatars.githubusercontent.com/u/808808?v=3","gravatar_id":"","url":"https://api.github.com/users/bendrucker","html_url":"https://github.com/bendrucker","followers_url":"https://api.github.com/users/bendrucker/followers","following_url":"https://api.github.com/users/bendrucker/following{/other_user}","gists_url":"https://api.github.com/users/bendrucker/gists{/gist_id}","starred_url":"https://api.github.com/users/bendrucker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bendrucker/subscriptions","organizations_url":"https://api.github.com/users/bendrucker/orgs","repos_url":"https://api.github.com/users/bendrucker/repos","events_url":"https://api.github.com/users/bendrucker/events{/privacy}","received_events_url":"https://api.github.com/users/bendrucker/received_events","type":"User","site_admin":false},"created_at":"2015-01-17T22:35:10Z","updated_at":"2015-01-17T22:35:10Z","body":"This is a bug. Join tables should be handled different when there's a through model."}}