Created
August 3, 2015 22:59
-
-
Save manlon/d17fdde1f97c4b8d03c6 to your computer and use it in GitHub Desktop.
repro case for issue with join tables in non-default db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
# repros in 4.x and on master | |
gem 'activerecord', '4.2.3' | |
#gem 'rails', github: 'rails/rails' | |
#gem 'arel', github: 'rails/arel' | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'primary.sqlite3') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# Abstract base class for models in the default database | |
class Primary < ActiveRecord::Base | |
self.abstract_class = true | |
end | |
# Abstract base class for models in the secondary database | |
class Secondary < ActiveRecord::Base | |
establish_connection(adapter: 'sqlite3', database: 'secondary.sqlite3') | |
self.abstract_class = true | |
end | |
ActiveRecord::Schema.define do | |
create_table :primary_models, force: true | |
create_table :primary_associateds, force: true | |
create_join_table :primary_associateds, :primary_models, force: true | |
end | |
# quick and dirty schema definition for the secondary db | |
sql = <<-EOSQL | |
DROP TABLE IF EXISTS "secondary_models" | |
DROP TABLE IF EXISTS "secondary_associateds" | |
DROP TABLE IF EXISTS "secondary_associateds_models" | |
CREATE TABLE "secondary_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) | |
CREATE TABLE "secondary_associateds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) | |
CREATE TABLE "secondary_associateds_models" ("secondary_model_id" INTEGER NOT NULL, "secondary_associated_id" INTEGER NOT NULL) | |
EOSQL | |
sql.split("\n").each{|line| Secondary.connection.execute line } | |
# A pair of HABTM-associated models in each db | |
class PrimaryModel < Primary | |
has_and_belongs_to_many :primary_associateds | |
end | |
class PrimaryAssociated < Primary; end | |
class SecondaryModel < Secondary | |
has_and_belongs_to_many :secondary_associateds | |
end | |
class SecondaryAssociated < Secondary; end | |
class JoinTableTest < Minitest::Test | |
def test_associations_primary | |
model = PrimaryModel.create! | |
associated = PrimaryAssociated.create! | |
assert_equal 0, model.primary_associateds.count | |
model.primary_associateds << associated | |
assert_equal 1, model.primary_associateds.reload.count | |
end | |
def test_associations_secondary | |
model = SecondaryModel.create! | |
associated = SecondaryAssociated.create! | |
# normal SELECT works | |
assert_equal 0, model.secondary_associateds.count | |
# adding to the association proxy raises: ActiveRecord::StatementInvalid: Could not find table 'secondary_associateds_models' | |
model.secondary_associateds << associated | |
assert_equal 1, model.secondary_associateds.reload.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment