From 30b6d4f7f16af78e3b361b8c52fcddca64cf9190 Mon Sep 17 00:00:00 2001 From: Rick Martinez Date: Fri, 10 Feb 2012 17:35:22 -0500 Subject: added test for #3732 --- activerecord/test/cases/multiple_db_test.rb | 12 ++++++++++++ activerecord/test/fixtures/colleges.yml | 3 +++ activerecord/test/fixtures/courses.yml | 1 + activerecord/test/models/arunit2_model.rb | 3 +++ activerecord/test/models/college.rb | 5 +++++ activerecord/test/models/course.rb | 5 ++++- activerecord/test/schema/schema.rb | 5 +++++ activerecord/test/support/connection.rb | 3 ++- 8 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/fixtures/colleges.yml create mode 100644 activerecord/test/models/arunit2_model.rb create mode 100644 activerecord/test/models/college.rb diff --git a/activerecord/test/cases/multiple_db_test.rb b/activerecord/test/cases/multiple_db_test.rb index e704322b5d..a802cfbf31 100644 --- a/activerecord/test/cases/multiple_db_test.rb +++ b/activerecord/test/cases/multiple_db_test.rb @@ -10,6 +10,7 @@ class MultipleDbTest < ActiveRecord::TestCase def setup @courses = create_fixtures("courses") { Course.retrieve_connection } + @colleges = create_fixtures("colleges") { College.retrieve_connection } @entrants = create_fixtures("entrants") end @@ -87,4 +88,15 @@ class MultipleDbTest < ActiveRecord::TestCase def test_arel_table_engines assert_equal Entrant.arel_engine, Bird.arel_engine end + + def test_associations_should_work_when_model_has_no_connection + begin + ActiveRecord::Model.remove_connection + assert_nothing_raised ActiveRecord::ConnectionNotEstablished do + College.first.courses.first + end + ensure + ActiveRecord::Model.establish_connection 'arunit' + end + end end diff --git a/activerecord/test/fixtures/colleges.yml b/activerecord/test/fixtures/colleges.yml new file mode 100644 index 0000000000..27591e0c2c --- /dev/null +++ b/activerecord/test/fixtures/colleges.yml @@ -0,0 +1,3 @@ +FIU: + id: 1 + name: Florida International University diff --git a/activerecord/test/fixtures/courses.yml b/activerecord/test/fixtures/courses.yml index 5ee1916003..de3a4a97e5 100644 --- a/activerecord/test/fixtures/courses.yml +++ b/activerecord/test/fixtures/courses.yml @@ -1,6 +1,7 @@ ruby: id: 1 name: Ruby Development + college: FIU java: id: 2 diff --git a/activerecord/test/models/arunit2_model.rb b/activerecord/test/models/arunit2_model.rb new file mode 100644 index 0000000000..04b8b15d3d --- /dev/null +++ b/activerecord/test/models/arunit2_model.rb @@ -0,0 +1,3 @@ +class ARUnit2Model < ActiveRecord::Base + self.abstract_class = true +end diff --git a/activerecord/test/models/college.rb b/activerecord/test/models/college.rb new file mode 100644 index 0000000000..c7495d7deb --- /dev/null +++ b/activerecord/test/models/college.rb @@ -0,0 +1,5 @@ +require_dependency 'models/arunit2_model' + +class College < ARUnit2Model + has_many :courses +end diff --git a/activerecord/test/models/course.rb b/activerecord/test/models/course.rb index 8a40fa740d..f3d0e05ff7 100644 --- a/activerecord/test/models/course.rb +++ b/activerecord/test/models/course.rb @@ -1,3 +1,6 @@ -class Course < ActiveRecord::Base +require_dependency 'models/arunit2_model' + +class Course < ARUnit2Model + belongs_to :college has_many :entrants end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index d473680fdc..428a85ab4e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -760,4 +760,9 @@ end Course.connection.create_table :courses, :force => true do |t| t.column :name, :string, :null => false + t.column :college_id, :integer +end + +College.connection.create_table :colleges, :force => true do |t| + t.column :name, :string, :null => false end diff --git a/activerecord/test/support/connection.rb b/activerecord/test/support/connection.rb index 60fea46fd3..11154c3797 100644 --- a/activerecord/test/support/connection.rb +++ b/activerecord/test/support/connection.rb @@ -1,4 +1,5 @@ require 'active_support/logger' +require_dependency 'models/college' require_dependency 'models/course' module ARTest @@ -15,6 +16,6 @@ module ARTest ActiveRecord::Model.logger = ActiveSupport::Logger.new("debug.log") ActiveRecord::Model.configurations = connection_config ActiveRecord::Model.establish_connection 'arunit' - Course.establish_connection 'arunit2' + ARUnit2Model.establish_connection 'arunit2' end end -- cgit v1.2.3 From 9f8b4d1510f66597bbe8732e507173217df9f14d Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Tue, 22 Nov 2011 10:43:03 +0100 Subject: fix associations when using per class databases would get ConnectionNotEstablished error because it always tried to use ActiveRecord::Base's connection, even though it should be using the connection of the model whose context we're operating in --- activerecord/lib/active_record/associations/alias_tracker.rb | 9 +++------ activerecord/lib/active_record/associations/association_scope.rb | 2 +- activerecord/lib/active_record/associations/join_dependency.rb | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 0248c7483c..84540a7000 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -5,12 +5,13 @@ module ActiveRecord # Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and # ActiveRecord::Associations::ThroughAssociationScope class AliasTracker # :nodoc: - attr_reader :aliases, :table_joins + attr_reader :aliases, :table_joins, :connection # table_joins is an array of arel joins which might conflict with the aliases we assign here - def initialize(table_joins = []) + def initialize(connection = ActiveRecord::Model.connection, table_joins = []) @aliases = Hash.new { |h,k| h[k] = initial_count_for(k) } @table_joins = table_joins + @connection = connection end def aliased_table_for(table_name, aliased_name = nil) @@ -70,10 +71,6 @@ module ActiveRecord def truncate(name) name.slice(0, connection.table_alias_length - 2) end - - def connection - ActiveRecord::Base.connection - end end end end diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 0209ce36df..982084c9b8 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -10,7 +10,7 @@ module ActiveRecord def initialize(association) @association = association - @alias_tracker = AliasTracker.new + @alias_tracker = AliasTracker.new klass.connection end def scope diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 827b01c5ac..cd366ac8b7 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -13,7 +13,7 @@ module ActiveRecord @join_parts = [JoinBase.new(base)] @associations = {} @reflections = [] - @alias_tracker = AliasTracker.new(joins) + @alias_tracker = AliasTracker.new(base.connection, joins) @alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1 build(associations) end -- cgit v1.2.3