From 1499cfabe75ed8145fe761adc4161c4be36767a6 Mon Sep 17 00:00:00 2001 From: "Suraj N. Kurapati" Date: Mon, 2 Dec 2013 10:58:58 -0800 Subject: [PATCH] keep sub-Rails connected to our in-memory database When another Rails instance is started by Capybara, it sets up a new connection to the test database instead of re-using our in-memory DB. http://www.spacevatican.org/2012/8/18/threading-the-rat/ --- README.md | 4 ++++ lib/tork/config/rails/master.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 8faac88..50f9e73 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,10 @@ database adapter][memory_test_fix]. Otherwise, you *might* face these errors: > cannot start a transaction within a transaction +For older Rails, to use SQLite3 as your test database along with browser-based +acceptance testing frameworks such as Capybara, see +http://www.spacevatican.org/2012/8/18/threading-the-rat/ + ## Configuration Tork looks for a configuration directory named `.tork/` inside its working diff --git a/lib/tork/config/rails/master.rb b/lib/tork/config/rails/master.rb index ff9af97..133edb6 100644 --- a/lib/tork/config/rails/master.rb +++ b/lib/tork/config/rails/master.rb @@ -31,6 +31,14 @@ class Tork::Railtie < Rails::Railtie if File.exist? seeds = "#{Rails.root}/db/seeds.rb" load seeds end + + # keep sub-Rails connected to in-memory database + # e.g. when another Rails is started by Capybara + # http://www.spacevatican.org/2012/8/18/threading-the-rat/ + class << ActiveRecord::Base + memory_database_connection = ActiveRecord::Base.connection + define_method(:connection) { memory_database_connection } + end end end end