Skip to content

Instantly share code, notes, and snippets.

@szimek
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szimek/8a219b126d29f7b68089 to your computer and use it in GitHub Desktop.
Save szimek/8a219b126d29f7b68089 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.0'
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 :posts do |t|
end
create_table :comments do |t|
t.integer :post_id
t.integer :comment_author_id
t.integer :order
end
create_table :comment_authors do |t|
t.string :name
end
end
class Post < ActiveRecord::Base
has_many :comments, -> { order(order: :asc) }
has_many :comment_authors, through: :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :comment_author
end
class CommentAuthor < ActiveRecord::Base
has_many :comments
has_many :posts, through: :comments
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
bob = CommentAuthor.create!(name: "Bob")
alice = CommentAuthor.create!(name: "Alice")
post.comments << Comment.create!(order: 1, comment_author: alice)
post.comments << Comment.create!(order: 2, comment_author: bob)
post_without_includes = Post.first
assert_equal(post_without_includes.comments.map(&:comment_author).map(&:name), post_without_includes.comment_authors.map(&:name))
post_with_includes = Post.includes(:comment_authors).first
assert_equal(post_with_includes.comments.map(&:comment_author).map(&:name), post_with_includes.comment_authors.map(&:name))
end
end
source "https://rubygems.org"
ruby "2.2.0"
gem "activerecord", "4.2.0"
gem "sqlite3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment