Skip to content

Instantly share code, notes, and snippets.

@dsimard
Created July 14, 2015 17:23
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 dsimard/c3bc2c8aebc3076d4e84 to your computer and use it in GitHub Desktop.
Save dsimard/c3bc2c8aebc3076d4e84 to your computer and use it in GitHub Desktop.
Paranoia
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.0'
gem 'sqlite3'
gem 'paranoia'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :invoices, force: true do |t|
end
create_table :lines, force: true do |t|
t.integer :invoice_id
t.integer :product_id
end
create_table :products, force: true do |t|
t.datetime :deleted_at
end
end
class Invoice < ActiveRecord::Base
has_many :lines
end
class Line < ActiveRecord::Base
belongs_to :invoice
belongs_to :product, ->{with_deleted}
end
class Product < ActiveRecord::Base
acts_as_paranoid
end
class BugTest < Minitest::Test
def test_association_stuff
# Create the invoice with the line linked on the product that will be deleted
invoice = Invoice.create!
product = Product.create!
invoice.lines.create! product: product
assert_equal 1, invoice.lines.count
# Delete the product
product.destroy!
# Use with `includes`
assert_equal product, Invoice.includes(lines: :product).last.lines.last.product
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment