Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relation merge turns an inner join into outer #22021

Closed
bughit opened this issue Oct 22, 2015 · 3 comments
Closed

relation merge turns an inner join into outer #22021

bughit opened this issue Oct 22, 2015 · 3 comments
Assignees

Comments

@bughit
Copy link
Contributor

bughit commented Oct 22, 2015

begin
  require 'bundler/inline'
rescue LoadError => e
  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true) do
  source 'https://rubygems.org'
  # Activate the gem you are reporting the issue against.
  gem 'activerecord', '4.2.4'
  gem 'sqlite3'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do

  create_table :blogs, force: true do |t|

  end

  create_table :posts, force: true do |t|
    t.belongs_to :blog
  end

  create_table :comments, force: true do |t|
    t.belongs_to :post

  end

end

class Blog < ActiveRecord::Base
  has_one :post

  scope :with_post_with_comment_good, ->{joins(post: :comment)}
  scope :with_post_with_comment_bad, ->{joins(:post).merge(Post.with_comment)}
end

class Post < ActiveRecord::Base
  belongs_to :blog
  has_one :comment

  scope :with_comment, ->{joins(:comment)}
end

class Comment < ActiveRecord::Base
  belongs_to :post
end


class BugTest < Minitest::Test
  def test_association_stuff

    Blog.create! post: Post.create!

    puts Blog.with_post_with_comment_good.to_sql
    puts Blog.with_post_with_comment_bad.to_sql

    assert_equal 0, Blog.with_post_with_comment_good.count
    assert_equal 0, Blog.with_post_with_comment_bad.count

  end
end
SELECT "blogs".* FROM "blogs" INNER JOIN "posts" ON "posts"."blog_id" = "blogs"."id" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
SELECT "blogs".* FROM "blogs" INNER JOIN "posts" ON "posts"."blog_id" = "blogs"."id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
@arthurnn arthurnn self-assigned this Oct 28, 2015
@maclover7
Copy link
Contributor

Reproduction script failing on master.

@sevaorlov
Copy link
Contributor

related to #24281

@stale
Copy link

stale bot commented Mar 28, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@stale stale bot added the stale label Mar 28, 2017
@stale stale bot closed this as completed Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants