Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created November 13, 2014 15:32
Show Gist options
  • Select an option

  • Save jturkel/b760ae7d9252b183268d to your computer and use it in GitHub Desktop.

Select an option

Save jturkel/b760ae7d9252b183268d to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.1.7'
gem 'destroyed_at'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'active_support/all'
require 'minitest/autorun'
require 'logger'
require 'destroyed_at'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Schema
ActiveRecord::Schema.define do
ActiveRecord::Base.connection.create_table(:posts) do |t|
end
ActiveRecord::Base.connection.create_table(:authors) do |t|
t.integer :post_id
end
end
# Models
class Post < ActiveRecord::Base
has_one :author, dependent: :destroy
end
class Author < ActiveRecord::Base
belongs_to :post
end
# The actual test
class BugTest < Minitest::Test
def test_has_one_dependent_destory
post = Post.create!
author = post.create_author!
post.destroy
assert_equal(0, Post.count)
assert_equal(0, Author.count)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment