Skip to content

Instantly share code, notes, and snippets.

@joelmoss
Created June 22, 2023 10:25
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 joelmoss/5522b429f53db7fd3d3e416c399c24d8 to your computer and use it in GitHub Desktop.
Save joelmoss/5522b429f53db7fd3d3e416c399c24d8 to your computer and use it in GitHub Desktop.
Ruby inline standalone script
# 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', '~> 7.0.5'
gem 'debug'
gem 'sqlite3'
end
require 'active_record'
require 'active_support/testing/assertions'
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 :appointments, force: true do |t|
t.boolean :paid, default: false
end
create_table :payments, force: true do |t|
t.references :appointment
end
end
class Appointment < ActiveRecord::Base
has_one :payment, dependent: :destroy
after_create do
logger.debug :ensure_payment
create_payment
end
end
class Payment < ActiveRecord::Base
belongs_to :appointment
before_create do
logger.debug :create_intent
appointment.assign_attributes paid: true
appointment.save validate: false
end
end
class BugTest < Minitest::Test
include ActiveSupport::Testing::Assertions
def test_bug
assert_difference 'Payment.count', 1 do
Appointment.create!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment