{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/4016","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/4016/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/4016/comments","events_url":"https://api.github.com/repos/rails/rails/issues/4016/events","html_url":"https://github.com/rails/rails/issues/4016","id":2591969,"number":4016,"title":"Duplicate joins created when using has_many :through and explicit joins","user":{"login":"dvandersluis","id":57964,"avatar_url":"https://avatars.githubusercontent.com/u/57964?v=3","gravatar_id":"","url":"https://api.github.com/users/dvandersluis","html_url":"https://github.com/dvandersluis","followers_url":"https://api.github.com/users/dvandersluis/followers","following_url":"https://api.github.com/users/dvandersluis/following{/other_user}","gists_url":"https://api.github.com/users/dvandersluis/gists{/gist_id}","starred_url":"https://api.github.com/users/dvandersluis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dvandersluis/subscriptions","organizations_url":"https://api.github.com/users/dvandersluis/orgs","repos_url":"https://api.github.com/users/dvandersluis/repos","events_url":"https://api.github.com/users/dvandersluis/events{/privacy}","received_events_url":"https://api.github.com/users/dvandersluis/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2011-12-18T06:10:37Z","updated_at":"2016-06-10T22:51:21Z","closed_at":"2011-12-18T17:55:45Z","body":"I'm not sure if this is related to #2556, but I'm using Rails 3.1.3 and experiencing this problem, so if it is, then it's not resolved still.\r\n\r\n    class Client < ActiveRecord::Base\r\n      has_many :jobs\r\n      has_many :job_applications, :through => :jobs\r\n    end\r\n    \r\n    class JobApplications\r\n      belongs_to :job\r\n    end\r\n    \r\n    irb(main):001:0> Client.find(10).job_applications.joins(:job)\r\n    ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'jobs': SELECT `job_applications`.* FROM `job_applications` INNER JOIN `jobs` ON `jobs`.`id` = `job_applications`.`job_id` INNER JOIN `jobs` ON `job_applications`.`job_id` = `jobs`.`id` WHERE `jobs`.`client_id` = 10\r\n\r\nAs you can see, ```INNER JOIN `jobs` ON `jobs`.`id` = `job_applications`.`job_id` ``` is getting duplicated."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/225314413","html_url":"https://github.com/rails/rails/issues/4016#issuecomment-225314413","issue_url":"https://api.github.com/repos/rails/rails/issues/4016","id":225314413,"user":{"login":"BM5k","id":29762,"avatar_url":"https://avatars.githubusercontent.com/u/29762?v=3","gravatar_id":"","url":"https://api.github.com/users/BM5k","html_url":"https://github.com/BM5k","followers_url":"https://api.github.com/users/BM5k/followers","following_url":"https://api.github.com/users/BM5k/following{/other_user}","gists_url":"https://api.github.com/users/BM5k/gists{/gist_id}","starred_url":"https://api.github.com/users/BM5k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BM5k/subscriptions","organizations_url":"https://api.github.com/users/BM5k/orgs","repos_url":"https://api.github.com/users/BM5k/repos","events_url":"https://api.github.com/users/BM5k/events{/privacy}","received_events_url":"https://api.github.com/users/BM5k/received_events","type":"User","site_admin":false},"created_at":"2016-06-10T22:51:21Z","updated_at":"2016-06-10T22:51:21Z","body":"Bit by this with today in rails 4.2.5.1.\r\n\r\nI think my solution will be to remove the explicit join from my scope and add it explicitly to every single call that needs it. For eternity. \r\n\r\nTo borrow the previous example:\r\n\r\n```\r\nscope :for, lambda { |course| joins(:badge_awards).where(['badge_awards.course_id = ?', course]) }\r\n```\r\nbecomes\r\n```\r\nscope :for, lambda { |course| where(['badge_awards.course_id = ?', course]) }\r\n```\r\n\r\n`Badge.for(course)` and `user.badges.for(course)` become \r\n`Badge.joins(badge_awards).for(course)` and `user.badges.for(course)`\r\n\r\nThis is harder to read, and will be more difficult to maintain long term. \r\n\r\n----\r\n\r\nI can see where @jonleighton is coming from with this comment\r\n\r\n> you are specifying that there are two joins\r\n\r\nbut I disagree. I think it was a stretch to say that the developer I'm  specifying _two_ joins. What I'm doing is specifying a relationship (that the framework handles) and a _single_ join. Rails is _helpfully_ providing the second join, and *ideally* should be smart enough to know when that's not necessary.\r\n\r\nAnother possible solution would be an idiomatic way for scopes to determine if the proper join already exists and piggyback on that. Then we could push the boilerplate into a single place (the scope definition) and then be free to use the scope as normal."}}
{"actor":{"display_login":"BM5k"}}