Skip to content

Instantly share code, notes, and snippets.

@slbug
Created December 2, 2014 10:21
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 slbug/5f0774da078639c67a48 to your computer and use it in GitHub Desktop.
Save slbug/5f0774da078639c67a48 to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails'
gem 'arel'
gem 'rack'
gem 'i18n'
gem 'sqlite3'
gem 'destroyed_at'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'destroyed_at'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.datetime :destroyed_at
end
create_table :comments do |t|
t.integer :post_id
t.datetime :destroyed_at
end
create_table :post_data do |t|
t.integer :post_id
t.datetime :destroyed_at
end
end
class Post < ActiveRecord::Base
include DestroyedAt
has_many :comments, dependent: :restrict_with_error
has_one :post_datum, inverse_of: :post, dependent: :destroy
end
class Comment < ActiveRecord::Base
include DestroyedAt
belongs_to :post
end
class PostDatum < ActiveRecord::Base
include DestroyedAt
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
Post.create!(post_datum: PostDatum.create!)
Post.last.destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment