I'm using AR outside of Rails and calling establish_connection on an ActiveRecord::Base abstract_class subclass (to protect my connection in case anyone else is using AR). It was working just fine with AR 3.0.9, but I recently attempted to upgrade to AR 3.2.2. Now, when I hit a "nested" query from a has_many, I get a ActiveRecord::ConnectionNotEstablished. If I call establish_connection on ActiveRecord::Base, instead of the DB abstract subclass, everything works fine.
I've stripped out a minimal example below, which should be self- contained, as well as a snippet that highlights the problem. Is this a bug, or does my code need to change to support AR 3.2 (also fails on 3.1).
This is the offending code:
Employer.all.each do |employer| p employer.employees end
Self-contained script:
#!/bin/env ruby
require 'sqlite3' require 'active_record' require 'logger'
ActiveRecord::Base.logger = Logger.new STDOUT
class DB < ActiveRecord::Base self.abstract_class = true end class Employer < DB has_many :employees end class Employee < DB end
# If establish_connection is instead called on AR::Base, this works fine. DB.establish_connection(:adapter => "sqlite3", :database => "./db.sqlite")
DB.connection.create_table(:employers) do |t| t.primary_key :id end DB.connection.create_table(:employees) do |t| t.primary_key :id t.integer :employer_id end
Employer.create!.employees << Employee.create!
Employer.all.each do |employer| p employer.employees end
Output:
D, [2012-03-02T16:22:36.852779 #14148] DEBUG -- : (1.7ms) select sqlite_version(*) D, [2012-03-02T16:22:36.854577 #14148] DEBUG -- : (1.3ms) CREATE TABLE "employers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) D, [2012-03-02T16:22:36.857462 #14148] DEBUG -- : (2.0ms) CREATE TABLE "employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "employer_id" integer) D, [2012-03-02T16:22:36.879564 #14148] DEBUG -- : (0.1ms) begin transaction D, [2012-03-02T16:22:36.884458 #14148] DEBUG -- : SQL (3.1ms) INSERT INTO "employers" VALUES(NULL) D, [2012-03-02T16:22:36.885693 #14148] DEBUG -- : (0.8ms) commit transaction D, [2012-03-02T16:22:36.956479 #14148] DEBUG -- : (0.1ms) begin transaction D, [2012-03-02T16:22:36.964279 #14148] DEBUG -- : SQL (5.8ms) INSERT INTO "employees" ("employer_id") VALUES (?) [["employer_id", nil]] D, [2012-03-02T16:22:36.965657 #14148] DEBUG -- : (0.9ms) commit transaction D, [2012-03-02T16:22:36.966902 #14148] DEBUG -- : (0.1ms) begin transaction D, [2012-03-02T16:22:36.969294 #14148] DEBUG -- : (0.5ms) UPDATE "employees" SET "employer_id" = 1 WHERE "employees"."id" = 1 D, [2012-03-02T16:22:36.972066 #14148] DEBUG -- : (2.3ms) commit transaction D, [2012-03-02T16:22:36.973350 #14148] DEBUG -- : Employer Load (0.3ms) SELECT "employers".* FROM "employers" /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/activerecord-3.2.2/ lib/active_record/connection_adapters/abstract/connection_pool.rb: 374:in `retrieve_connection': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished) from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/connection_adapters/abstract/ connection_specification.rb:168:in `retrieve_connection' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/connection_adapters/abstract/ connection_specification.rb:142:in `connection' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 75:in `connection' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 54:in `initial_count_for' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 12:in `block in initialize' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 29:in `yield' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 29:in `default' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 29:in `aliased_name_for' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb: 17:in `aliased_table_for' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/join_helper.rb:15:in `block in construct_tables' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `each' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `construct_tables' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/association_scope.rb: 37:in `add_constraints' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/association_scope.rb: 31:in `scope' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/association.rb:98:in `association_scope' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/ collection_association.rb:380:in `find_target' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/ collection_association.rb:333:in `load_target' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb: 44:in `load_target' from /Users/mlartz/.rvm/gems/ruby-1.9.3-p0@tmp/gems/ activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb: 87:in `method_missing' from test.rb:38:in `p' from test.rb:38:in `block in <main>' from test.rb:37:in `each' from test.rb:37:in `<main>'
Any ideas?