Skip to content

Instantly share code, notes, and snippets.

@congwenma
Last active April 6, 2017 19:48
Show Gist options
  • Save congwenma/c70fe6284cc4c1b93ee867b93f40c4e4 to your computer and use it in GitHub Desktop.
Save congwenma/c70fe6284cc4c1b93ee867b93f40c4e4 to your computer and use it in GitHub Desktop.
Single file rails application
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
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|
t.string :title
t.integer :lock_version, default: 0
t.timestamps
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!(title: 'Original Title')
post_copy = Post.find(post.id)
post.update!(title: 'New Title')
post.update!(title: 'Another New Title')
post_version = post.lock_version
# This line should probably raise ActiveRecord::StaleObjectError,
# but does not.
post_copy.touch
# And, surely, the lock_version should never go backwards.
assert post.reload.lock_version >= post_version
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment