Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Last active June 17, 2021 14:39
Show Gist options
  • Save ghiculescu/1beb8c08952a397b637c08e6062e17f9 to your computer and use it in GitHub Desktop.
Save ghiculescu/1beb8c08952a397b637c08e6062e17f9 to your computer and use it in GitHub Desktop.
#13071
# 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", github: "rails/rails", branch: "main"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# 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 :authors, force: true do |t|
end
create_table :posts, force: true do |t|
t.bigint :num
t.references :author
end
end
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
include Comparable
belongs_to :author
# if you uncomment this method, it'll get used. but if you leave it commented,
# <=> is used. if you remove the `Comparable` include, then `<=>` *never* gets called.
# def ==(other)
# puts "called =="
# num == other.num
# end
def <=>(other)
puts "called <=>"
num - other.num
end
end
class BugTest < Minitest::Test
def test_association_stuff
author = Author.new
Post.create!(num: 1, author: author)
Post.create!(num: 2, author: author)
Post.create!(num: 3, author: author)
author.posts.build(num: 4)
author.posts.build(num: 5)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment