{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/33379","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/33379/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/33379/comments","events_url":"https://api.github.com/repos/rails/rails/issues/33379/events","html_url":"https://github.com/rails/rails/issues/33379","id":341974516,"node_id":"MDU6SXNzdWUzNDE5NzQ1MTY=","number":33379,"title":"[ActiveRecord] Issue with joins && merges","user":{"login":"gustavolobo","id":932251,"node_id":"MDQ6VXNlcjkzMjI1MQ==","avatar_url":"https://avatars3.githubusercontent.com/u/932251?v=4","gravatar_id":"","url":"https://api.github.com/users/gustavolobo","html_url":"https://github.com/gustavolobo","followers_url":"https://api.github.com/users/gustavolobo/followers","following_url":"https://api.github.com/users/gustavolobo/following{/other_user}","gists_url":"https://api.github.com/users/gustavolobo/gists{/gist_id}","starred_url":"https://api.github.com/users/gustavolobo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gustavolobo/subscriptions","organizations_url":"https://api.github.com/users/gustavolobo/orgs","repos_url":"https://api.github.com/users/gustavolobo/repos","events_url":"https://api.github.com/users/gustavolobo/events{/privacy}","received_events_url":"https://api.github.com/users/gustavolobo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-07-17T15:37:47Z","updated_at":"2018-07-18T17:04:45Z","closed_at":null,"author_association":"NONE","body":"### Steps to reproduce\r\n\r\n```ruby\r\nbegin\r\n  require 'bundler/inline'\r\nrescue LoadError => e\r\n  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'\r\n  raise e\r\nend\r\n\r\ngemfile(true) do\r\n  source 'https://rubygems.org'\r\n  gem 'activerecord', '5.2.0'\r\n  gem 'sqlite3'\r\nend\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\nrequire 'logger'\r\n\r\n# This connection will do for database-independent bug reports.\r\nActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :users, force: true  do |t|\r\n    t.string :name\r\n    t.boolean :locked\r\n  end\r\n\r\n  create_table :blogger_profiles, force: true  do |t|\r\n    t.integer :user_id\r\n    t.boolean :verified\r\n  end\r\nend\r\n\r\nclass ApplicationRecord < ActiveRecord::Base\r\n  self.abstract_class = true\r\nend\r\n\r\nclass User < ApplicationRecord\r\n  has_many :blogger_profiles\r\n\r\n  scope :not_locked, -> { where(locked: false) }\r\n  scope :verified, -> { joins(:blogger_profiles).merge(BloggerProfile.verified).distinct }\r\nend\r\n\r\nclass BloggerProfile < ApplicationRecord\r\n  belongs_to :user\r\n\r\n  scope :verified, -> { joins(:user).where(verified: true).merge(User.not_locked) }\r\nend\r\n\r\nclass BugTest < Minitest::Test\r\n  def test_query_stuff\r\n    User.verified.to_a\r\n  end\r\nend\r\n```\r\n\r\n### Expected behavior\r\nTo not raise an exception.\r\n\r\n### Actual behavior\r\nRaises an exception in 5.2.0 (`ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: main.users.id`), it works in 5.1.6.\r\n\r\nThe query generated in 5.1.6:\r\n```sql\r\nSELECT DISTINCT \"users\".* FROM \"users\"\r\nINNER JOIN \"blogger_profiles\" ON \"blogger_profiles\".\"user_id\" = \"users\".\"id\" \r\nLEFT OUTER JOIN \"users\" \"users_blogger_profiles\" ON \"users_blogger_profiles\".\"id\" = \"blogger_profiles\".\"user_id\" \r\nWHERE \"blogger_profiles\".\"verified\" = ? AND \"users\".\"locked\" = ?\r\n```\r\n\r\nThe query generated in 5.2.0:\r\n```sql\r\nSELECT DISTINCT \"users\".* FROM \"users\" \r\nINNER JOIN \"blogger_profiles\" ON \"blogger_profiles\".\"user_id\" = \"users\".\"id\" \r\nINNER JOIN \"users\" ON \"users\".\"id\" = \"blogger_profiles\".\"user_id\" \r\nWHERE \"blogger_profiles\".\"verified\" = ? AND \"users\".\"locked\" = ?\r\n```"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/406004478","html_url":"https://github.com/rails/rails/issues/33379#issuecomment-406004478","issue_url":"https://api.github.com/repos/rails/rails/issues/33379","id":406004478,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNjAwNDQ3OA==","user":{"login":"twitnithegirl","id":3744370,"node_id":"MDQ6VXNlcjM3NDQzNzA=","avatar_url":"https://avatars0.githubusercontent.com/u/3744370?v=4","gravatar_id":"","url":"https://api.github.com/users/twitnithegirl","html_url":"https://github.com/twitnithegirl","followers_url":"https://api.github.com/users/twitnithegirl/followers","following_url":"https://api.github.com/users/twitnithegirl/following{/other_user}","gists_url":"https://api.github.com/users/twitnithegirl/gists{/gist_id}","starred_url":"https://api.github.com/users/twitnithegirl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twitnithegirl/subscriptions","organizations_url":"https://api.github.com/users/twitnithegirl/orgs","repos_url":"https://api.github.com/users/twitnithegirl/repos","events_url":"https://api.github.com/users/twitnithegirl/events{/privacy}","received_events_url":"https://api.github.com/users/twitnithegirl/received_events","type":"User","site_admin":false},"created_at":"2018-07-18T17:04:45Z","updated_at":"2018-07-18T17:04:45Z","author_association":"NONE","body":"@gustavolobo you can [fork the repo, fix the bug, and submit a PR with your patch](http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-code) however if you aren't able to do that I can probably start investigating it tomorrow and see what I can come up with."}}
{"actor":{"display_login":"twitnithegirl"}}