mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #5000 from flavorpill/master-with-multidb-association-fix
Fix associations with per-class/multiple database connections
This commit is contained in:
commit
04c0aea47a
11 changed files with 40 additions and 10 deletions
|
@ -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
|
||||
|
|
|
@ -10,7 +10,7 @@ module ActiveRecord
|
|||
|
||||
def initialize(association)
|
||||
@association = association
|
||||
@alias_tracker = AliasTracker.new
|
||||
@alias_tracker = AliasTracker.new klass.connection
|
||||
end
|
||||
|
||||
def scope
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
3
activerecord/test/fixtures/colleges.yml
vendored
Normal file
3
activerecord/test/fixtures/colleges.yml
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
FIU:
|
||||
id: 1
|
||||
name: Florida International University
|
1
activerecord/test/fixtures/courses.yml
vendored
1
activerecord/test/fixtures/courses.yml
vendored
|
@ -1,6 +1,7 @@
|
|||
ruby:
|
||||
id: 1
|
||||
name: Ruby Development
|
||||
college: FIU
|
||||
|
||||
java:
|
||||
id: 2
|
||||
|
|
3
activerecord/test/models/arunit2_model.rb
Normal file
3
activerecord/test/models/arunit2_model.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class ARUnit2Model < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
end
|
5
activerecord/test/models/college.rb
Normal file
5
activerecord/test/models/college.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require_dependency 'models/arunit2_model'
|
||||
|
||||
class College < ARUnit2Model
|
||||
has_many :courses
|
||||
end
|
|
@ -1,3 +1,6 @@
|
|||
class Course < ActiveRecord::Base
|
||||
require_dependency 'models/arunit2_model'
|
||||
|
||||
class Course < ARUnit2Model
|
||||
belongs_to :college
|
||||
has_many :entrants
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue