Skip to content

Commit

Permalink
Merge pull request #5000 from flavorpill/master-with-multidb-associat…
Browse files Browse the repository at this point in the history
…ion-fix

Fix associations with per-class/multiple database connections
  • Loading branch information
tenderlove committed Feb 13, 2012
2 parents 3a5a01f + 9f8b4d1 commit 04c0aea
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 10 deletions.
9 changes: 3 additions & 6 deletions activerecord/lib/active_record/associations/alias_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module Associations
# 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)
Expand Down Expand Up @@ -70,10 +71,6 @@ def initial_count_for(name)
def truncate(name)
name.slice(0, connection.table_alias_length - 2)
end

def connection
ActiveRecord::Base.connection
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AssociationScope #:nodoc:

def initialize(association)
@association = association
@alias_tracker = AliasTracker.new
@alias_tracker = AliasTracker.new klass.connection
end

def scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(base, associations, joins)
@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
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/multiple_db_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -87,4 +88,15 @@ def test_transactions_across_databases
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
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/colleges.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FIU:
id: 1
name: Florida International University
1 change: 1 addition & 0 deletions activerecord/test/fixtures/courses.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ruby:
id: 1
name: Ruby Development
college: FIU

java:
id: 2
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/arunit2_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ARUnit2Model < ActiveRecord::Base
self.abstract_class = true
end
5 changes: 5 additions & 0 deletions activerecord/test/models/college.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_dependency 'models/arunit2_model'

class College < ARUnit2Model
has_many :courses
end
5 changes: 4 additions & 1 deletion activerecord/test/models/course.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Course < ActiveRecord::Base
require_dependency 'models/arunit2_model'

class Course < ARUnit2Model
belongs_to :college
has_many :entrants
end
5 changes: 5 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,9 @@ def create_table(*args, &block)

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
3 changes: 2 additions & 1 deletion activerecord/test/support/connection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_support/logger'
require_dependency 'models/college'
require_dependency 'models/course'

module ARTest
Expand All @@ -15,6 +16,6 @@ def self.connect
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

0 comments on commit 04c0aea

Please sign in to comment.