Last active
June 26, 2017 01:53
-
-
Save dicemanx/0ba9f227613600246178ab9537265a0a to your computer and use it in GitHub Desktop.
Reproduction of a possible bug in ActiveRecord_AssociationRelation#or
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "5.1.0" | |
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 :users, force: true do |t| | |
end | |
create_table :publications, force: true do |t| | |
end | |
create_table :user_publications, force: true do |t| | |
t.integer :user_id | |
t.integer :publication_id | |
end | |
create_table :comments, force: true do |t| | |
t.integer :publication_id | |
t.integer :user_id | |
end | |
end | |
class User < ActiveRecord::Base | |
has_many :user_publications | |
has_many :publications, through: :user_publications | |
has_many :comments | |
end | |
class Publication < ActiveRecord::Base | |
has_many :user_publications | |
has_many :users, through: :user_publications | |
has_many :comments | |
end | |
class UserPublication < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :publication | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :publication | |
belongs_to :user | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
user = User.create! | |
publication1 = Publication.create! | |
publication2 = Publication.create! | |
publication3 = Publication.create! | |
user.publications << publication1 | |
user.publications << publication2 | |
user.publications << publication3 | |
publication1.comments << Comment.create! | |
publication2.comments << Comment.create! | |
publication3.comments << Comment.create! | |
relevant_comments_relation = Comment.none | |
relevant_comments_array = [] | |
user.publications.each do |publication| | |
relevant_comments_relation = relevant_comments_relation.or(publication.comments) | |
relevant_comments_array = relevant_comments_array + publication.comments | |
end | |
assert_equal relevant_comments_array, relevant_comments_relation | |
assert_equal relevant_comments_array.last, relevant_comments_relation.last | |
assert_equal relevant_comments_array.last.publication, relevant_comments_relation.last.publication | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment