Created
March 24, 2023 13:39
-
-
Save youknowcast/9ca47aac7625ea8e7adde9d3b26adfd3 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. | |
gem "activerecord", "6.1.7.3" | |
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 :posts, force: true do |t| | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
t.string :text | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def test_replace1 | |
post = Post.create! | |
["no.1", "no.2", "no.3"].each do |s| | |
post.comments << Comment.create!(text: s) | |
end | |
post = Post.first | |
updated = post.comments[2] | |
updated.text = "no.2" | |
# try to no.1 -> replaced by new record | |
# try to no.2 -> remove | |
# try to no.3 -> no.2 | |
post.comments.replace([updated, Comment.new(text: "no.1")]) | |
#p Post.first.comments | |
assert_equal [3, 4], Post.first.comments.order(:id).pluck(:id) | |
# no changes for existing record | |
assert_equal "no.3", Post.first.comments.find(updated.id).text | |
assert_equal "no.1", Post.first.comments.order(:id).last.text | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment