diff options
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/mentor.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/project.rb | 1 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 6 |
6 files changed, 29 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 81eb5136a1..ca473beea3 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -131,9 +131,9 @@ module ActiveRecord def instantiate(result_set, aliases) primary_key = aliases.column_alias(join_root, join_root.primary_key) - seen = Hash.new { |h,parent_klass| - h[parent_klass] = Hash.new { |i,parent_id| - i[parent_id] = Hash.new { |j,child_klass| j[child_klass] = {} } + seen = Hash.new { |i, object_id| + i[object_id] = Hash.new { |j, child_class| + j[child_class] = {} } } @@ -233,7 +233,6 @@ module ActiveRecord def construct(ar_parent, parent, row, rs, seen, model_cache, aliases) return if ar_parent.nil? - primary_id = ar_parent.id parent.children.each do |node| if node.reflection.collection? @@ -253,14 +252,14 @@ module ActiveRecord next end - model = seen[parent.base_klass][primary_id][node.base_klass][id] + model = seen[ar_parent.object_id][node.base_klass][id] if model construct(model, node, row, rs, seen, model_cache, aliases) else model = construct_model(ar_parent, node, row, model_cache, id, aliases) model.readonly! - seen[parent.base_klass][primary_id][node.base_klass][id] = model + seen[ar_parent.object_id][node.base_klass][id] = model construct(model, node, row, rs, seen, model_cache, aliases) end end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index ddfb856a05..bc1bff79d3 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -24,6 +24,8 @@ require 'models/membership' require 'models/club' require 'models/categorization' require 'models/sponsor' +require 'models/mentor' +require 'models/contract' class EagerAssociationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :essays, :author_addresses, :categories, :categories_posts, @@ -1218,6 +1220,16 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_no_queries { assert_equal 2, posts[1].categories[0].categorizations.length } end + def test_eager_load_multiple_associations_with_references + mentor = Mentor.create!(name: "Barış Can DAYLIK") + developer = Developer.create!(name: "Mehmet Emin İNAÇ", mentor: mentor) + Contract.create!(developer: developer) + project = Project.create!(name: "VNGRS", mentor: mentor) + project.developers << developer + projects = Project.references(:mentors).includes(mentor: { developers: :contracts }, developers: :contracts) + assert_equal projects.last.mentor.developers.first.contracts, projects.last.developers.last.contracts + end + test "scoping with a circular preload" do assert_equal Comment.find(1), Comment.preload(:post => :comments).scoping { Comment.find(1) } end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 7c5941b1af..9a907273f8 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -15,6 +15,8 @@ class Developer < ActiveRecord::Base end end + belongs_to :mentor + accepts_nested_attributes_for :projects has_and_belongs_to_many :shared_computers, class_name: "Computer" diff --git a/activerecord/test/models/mentor.rb b/activerecord/test/models/mentor.rb new file mode 100644 index 0000000000..11f1e4bff8 --- /dev/null +++ b/activerecord/test/models/mentor.rb @@ -0,0 +1,3 @@ +class Mentor < ActiveRecord::Base + has_many :developers +end
\ No newline at end of file diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 5328330653..b034e0e267 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -1,4 +1,5 @@ class Project < ActiveRecord::Base + belongs_to :mentor has_and_belongs_to_many :developers, -> { distinct.order 'developers.name desc, developers.id desc' } has_and_belongs_to_many :readonly_developers, -> { readonly }, :class_name => "Developer" has_and_belongs_to_many :non_unique_developers, -> { order 'developers.name desc, developers.id desc' }, :class_name => 'Developer' diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index d334a2740e..690ab1af4b 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -253,6 +253,7 @@ ActiveRecord::Schema.define do t.string :first_name t.integer :salary, default: 70000 t.integer :firm_id + t.integer :mentor_id if subsecond_precision_supported? t.datetime :created_at, precision: 6 t.datetime :updated_at, precision: 6 @@ -452,6 +453,10 @@ ActiveRecord::Schema.define do t.string :name end + create_table :mentors, force: true do |t| + t.string :name + end + create_table :minivans, force: true, id: false do |t| t.string :minivan_id t.string :name @@ -660,6 +665,7 @@ ActiveRecord::Schema.define do t.string :name t.string :type t.integer :firm_id + t.integer :mentor_id end create_table :randomly_named_table1, force: true do |t| |