提交 9f8b4d15 编写于 作者: L Lars Kanis 提交者: Rick Martinez

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
上级 30b6d4f7
...@@ -5,12 +5,13 @@ module Associations ...@@ -5,12 +5,13 @@ module Associations
# Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and # Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and
# ActiveRecord::Associations::ThroughAssociationScope # ActiveRecord::Associations::ThroughAssociationScope
class AliasTracker # :nodoc: 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 # 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) } @aliases = Hash.new { |h,k| h[k] = initial_count_for(k) }
@table_joins = table_joins @table_joins = table_joins
@connection = connection
end end
def aliased_table_for(table_name, aliased_name = nil) def aliased_table_for(table_name, aliased_name = nil)
...@@ -70,10 +71,6 @@ def initial_count_for(name) ...@@ -70,10 +71,6 @@ def initial_count_for(name)
def truncate(name) def truncate(name)
name.slice(0, connection.table_alias_length - 2) name.slice(0, connection.table_alias_length - 2)
end end
def connection
ActiveRecord::Base.connection
end
end end
end end
end end
...@@ -10,7 +10,7 @@ class AssociationScope #:nodoc: ...@@ -10,7 +10,7 @@ class AssociationScope #:nodoc:
def initialize(association) def initialize(association)
@association = association @association = association
@alias_tracker = AliasTracker.new @alias_tracker = AliasTracker.new klass.connection
end end
def scope def scope
......
...@@ -13,7 +13,7 @@ def initialize(base, associations, joins) ...@@ -13,7 +13,7 @@ def initialize(base, associations, joins)
@join_parts = [JoinBase.new(base)] @join_parts = [JoinBase.new(base)]
@associations = {} @associations = {}
@reflections = [] @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 @alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1
build(associations) build(associations)
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册