-
-
Save kamipo/480f9e6fb61bef0f36b1edbccfd30f66 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" } | |
gem "activerecord", "6.1.0" | |
gem "mysql2" | |
end | |
require "active_record" | |
require "logger" | |
ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "test", username: "root") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :users, force: true do |t| | |
t.string :name, index: { unique: true } | |
end | |
end | |
class User < ActiveRecord::Base | |
end | |
t = Thread.new do | |
sleep 0.1 | |
User.create!(name: "foo") | |
end | |
User.transaction do | |
User.find_by!(name: "foo") | |
rescue ActiveRecord::RecordNotFound | |
puts 'User<name: "foo"> not found' | |
sleep 0.2 | |
User.create_or_find_by!(name: "foo").tap do | |
puts 'User<name: "foo"> has found or created' | |
end | |
end | |
t.join |
Author
kamipo
commented
Dec 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment