aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb33
-rw-r--r--activerecord/test/associations_join_model_test.rb7
-rw-r--r--activerecord/test/fixtures/author.rb6
-rw-r--r--activerecord/test/fixtures/db_definitions/schema.rb5
5 files changed, 38 insertions, 15 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e92cf57e4f..d34aef9477 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) [jonathan@bluewire.net.nz]
+
* Quit ignoring default :include options in has_many :through calls [Mark James]
* Allow has_many :through associations to find the source association by setting a custom class (closes #4307) [jonathan@bluewire.net.nz]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 14a3eeb1eb..6f149d16e0 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1310,28 +1310,27 @@ module ActiveRecord
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias,
aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
]
- elsif source_reflection.macro == :belongs_to # has_many :through against a belongs_to
- " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
- table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
- through_reflection.primary_key_name,
- parent.aliased_table_name, parent.primary_key] +
- " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
- table_name_and_alias,
- aliased_table_name, primary_key,
- aliased_join_table_name, options[:foreign_key] || klass.to_s.classify.foreign_key
- ]
- elsif source_reflection.macro == :has_many # has_many :through against a has_many
+ else
+ case source_reflection.macro
+ when :belongs_to
+ first_key = primary_key
+ second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
+ when :has_many
+ first_key = through_reflection.klass.to_s.classify.foreign_key
+ second_key = options[:foreign_key] || primary_key
+ end
+
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
through_reflection.primary_key_name,
parent.aliased_table_name, parent.primary_key] +
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias,
- aliased_table_name, through_reflection.klass.to_s.classify.foreign_key,
- aliased_join_table_name, options[:foreign_key] || primary_key
+ aliased_table_name, first_key,
+ aliased_join_table_name, second_key
]
end
-
+
when reflection.macro == :has_many && reflection.options[:as]
" LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s" % [
table_name_and_alias,
@@ -1342,9 +1341,13 @@ module ActiveRecord
]
else
+ foreign_key = options[:foreign_key] || case reflection.macro
+ when :has_many then reflection.active_record.to_s.classify
+ when :has_one then reflection.active_record.to_s
+ end.foreign_key
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias,
- aliased_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key,
+ aliased_table_name, foreign_key,
parent.aliased_table_name, parent.primary_key
]
end
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb
index d7a230ddba..ee99d77931 100644
--- a/activerecord/test/associations_join_model_test.rb
+++ b/activerecord/test/associations_join_model_test.rb
@@ -287,6 +287,13 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
end
end
+ def test_eager_belongs_to_and_has_one_not_singularized
+ assert_nothing_raised do
+ Author.find(:first, :include => :author_address)
+ AuthorAddress.find(:first, :include => :author)
+ end
+ end
+
private
# create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency)
diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb
index 1dda0f837c..e510dd1992 100644
--- a/activerecord/test/fixtures/author.rb
+++ b/activerecord/test/fixtures/author.rb
@@ -28,6 +28,8 @@ class Author < ActiveRecord::Base
has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
+ belongs_to :author_address
+
attr_accessor :post_log
def after_initialize
@@ -55,3 +57,7 @@ class Author < ActiveRecord::Base
raise Exception.new("You can't add a post")
end
end
+
+class AuthorAddress < ActiveRecord::Base
+ has_one :author
+end \ No newline at end of file
diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb
index efe9cb35ab..6e8e23a9c4 100644
--- a/activerecord/test/fixtures/db_definitions/schema.rb
+++ b/activerecord/test/fixtures/db_definitions/schema.rb
@@ -19,5 +19,10 @@ ActiveRecord::Schema.define do
end
add_column :posts, :taggings_count, :integer, :default => 0
+ add_column :authors, :author_address_id, :integer
+
+ create_table :author_addresses, :force => true do |t|
+ t.column :author_address_id, :integer
+ end
end \ No newline at end of file