From 6050d0177c3b9dc11c3e3c3dfa17530e27d90996 Mon Sep 17 00:00:00 2001 From: Kelley Reynolds Date: Thu, 9 Feb 2012 13:51:40 -0500 Subject: Fix eagerly loading associations without primary keys --- .../active_record/associations/join_dependency/join_part.rb | 2 +- activerecord/test/cases/associations/eager_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/join_dependency/join_part.rb b/activerecord/lib/active_record/associations/join_dependency/join_part.rb index 2b1d888a9a..711f7b3ce1 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_part.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_part.rb @@ -54,7 +54,7 @@ module ActiveRecord unless @column_names_with_alias @column_names_with_alias = [] - ([primary_key] + (column_names - [primary_key])).each_with_index do |column_name, i| + ([primary_key] + (column_names - [primary_key])).compact.each_with_index do |column_name, i| @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"] end end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index b79c69bbb5..35070ba506 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -39,6 +39,19 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_nil member.favourite_club end + def test_loading_with_one_association_without_primary_key + Comment.primary_key = nil + begin + assert_nothing_raised do + Post.includes(:comments).references(:comments).all + end + rescue + raise $! + ensure + Comment.primary_key = 'id' + end + end + def test_loading_with_one_association posts = Post.find(:all, :include => :comments) post = posts.find { |p| p.id == 1 } -- cgit v1.2.3 From 07dad778d18b45eb6d6a003d3ba7cb8528e9c720 Mon Sep 17 00:00:00 2001 From: Kelley Reynolds Date: Thu, 5 Jul 2012 11:23:01 -0400 Subject: Change JoinPart test from an integration to a unit test --- activerecord/test/cases/associations/eager_test.rb | 13 ------------- .../test/cases/associations/join_dependency_test.rb | 8 ++++++++ 2 files changed, 8 insertions(+), 13 deletions(-) create mode 100644 activerecord/test/cases/associations/join_dependency_test.rb (limited to 'activerecord') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 35070ba506..b79c69bbb5 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -39,19 +39,6 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_nil member.favourite_club end - def test_loading_with_one_association_without_primary_key - Comment.primary_key = nil - begin - assert_nothing_raised do - Post.includes(:comments).references(:comments).all - end - rescue - raise $! - ensure - Comment.primary_key = 'id' - end - end - def test_loading_with_one_association posts = Post.find(:all, :include => :comments) post = posts.find { |p| p.id == 1 } diff --git a/activerecord/test/cases/associations/join_dependency_test.rb b/activerecord/test/cases/associations/join_dependency_test.rb new file mode 100644 index 0000000000..08c166dc33 --- /dev/null +++ b/activerecord/test/cases/associations/join_dependency_test.rb @@ -0,0 +1,8 @@ +require "cases/helper" +require 'models/edge' + +class JoinDependencyTest < ActiveRecord::TestCase + def test_column_names_with_alias_handles_nil_primary_key + assert_equal Edge.column_names, ActiveRecord::Associations::JoinDependency::JoinBase.new(Edge).column_names_with_alias.map(&:first) + end +end \ No newline at end of file -- cgit v1.2.3