{"action":"closed","issue":{"url":"https://api.github.com/repos/rails/rails/issues/24674","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/24674/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/24674/comments","events_url":"https://api.github.com/repos/rails/rails/issues/24674/events","html_url":"https://github.com/rails/rails/issues/24674","id":150050329,"number":24674,"title":"Adding includes to a scoped has_many through produces SQL missing table in FROM clause","user":{"login":"speckins","id":522886,"avatar_url":"https://avatars.githubusercontent.com/u/522886?v=3","gravatar_id":"","url":"https://api.github.com/users/speckins","html_url":"https://github.com/speckins","followers_url":"https://api.github.com/users/speckins/followers","following_url":"https://api.github.com/users/speckins/following{/other_user}","gists_url":"https://api.github.com/users/speckins/gists{/gist_id}","starred_url":"https://api.github.com/users/speckins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/speckins/subscriptions","organizations_url":"https://api.github.com/users/speckins/orgs","repos_url":"https://api.github.com/users/speckins/repos","events_url":"https://api.github.com/users/speckins/events{/privacy}","received_events_url":"https://api.github.com/users/speckins/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-21T11:46:13Z","updated_at":"2016-04-21T12:11:32Z","closed_at":"2016-04-21T12:11:32Z","body":"Test case available [here](https://gist.github.com/speckins/b27e112d0062039778f7772905b96559).\r\n\r\n### Steps to reproduce\r\nI ran across this behavior when attempting to create a scoped has_many through association.  The association itself works as expected when called on an instance.  However, if I attempt to eager load the scoped association using includes, the produced SQL references a table in the WHERE clause that is not present in the FROM clause.\r\n\r\n```ruby\r\n# User <- Post <- Comment -> CommentType\r\n\r\nclass User < ActiveRecord::Base\r\n  has_many :posts\r\n\r\n  has_many :comments, :through => :posts\r\n  # This is the problem association:\r\n  has_many :funny_comments, -> { funny }, :through => :posts, :source => :comments\r\nend\r\n\r\nclass Post < ActiveRecord::Base\r\n  has_many :comments\r\nend\r\n\r\nclass Comment < ActiveRecord::Base\r\n  belongs_to :comment_type\r\n\r\n  scope :funny, -> { joins(:comment_type).merge(CommentType.funny) }\r\n  # This fails, too, btw:\r\n  #scope :funny, -> { joins(:comment_type).where comment_types: {comment_type: 'funny'} }\r\nend\r\n\r\nclass CommentType < ActiveRecord::Base\r\n  scope :funny, -> { where :comment_type => 'funny' }\r\nend\r\n```\r\n\r\n```ruby\r\nuser = User.first\r\nuser.funny_comments # This works\r\nUser.includes(:funny_comments).limit(2) # This fails\r\n```\r\n\r\n**ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: comment_types.comment_type: SELECT \"posts\".*, \"posts\".\"id\" AS t0_r0, \"posts\".\"user_id\" AS t0_r1, \"comments\".\"id\" AS t1_r0, \"comments\".\"post_id\" AS t1_r1, \"comments\".\"comment_type_id\" AS t1_r2 FROM \"posts\" LEFT OUTER JOIN \"comments\" ON \"comments\".\"post_id\" = \"posts\".\"id\" WHERE \"comment_types\".\"comment_type\" = ? AND \"posts\".\"user_id\" IN (1)**\r\n\r\n### System configuration\r\n**Rails version**:  4.2.6, 5.0.0-beta3\r\n\r\n**Ruby version**: 2.2.1, 2.2.4\r\n**Databases**: Postgres, SQLite3\r\n\r\n\r\nIf anyone can suggest a workaround in the meantime, I'd appreciate it."}}