Skip to content

Instantly share code, notes, and snippets.

@intrip
Created March 26, 2021 15:07
Show Gist options
  • Save intrip/c5ca39e0f4d49e1f91cb76f3c78f2dca to your computer and use it in GitHub Desktop.
Save intrip/c5ca39e0f4d49e1f91cb76f3c78f2dca to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", path: "."
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "activerecord_unittest")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
post2 = Post.create!
post3 = Post.create!
post.comments += [Comment.create!, Comment.create!, Comment.create!]
post2.comments += [Comment.create!,Comment.create!]
post3.comments += [Comment.create!]
assert_equal 3, post.comments.count
assert_equal 6, Comment.count
assert_equal({ 1 => 3, 2 => 2, 3 => 1 }, Comment.group(:post_id).count)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment