Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't classify the has_one class when eager loading, it is already si…

…ngular. Add tests. (closes #4117) [jonathan@bluewire.net.nz]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3983 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9a72cd22cf427754a30af409c4651da2017296a8 1 parent 48e7edc
@technoweenie technoweenie authored
View
2  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]
View
33 activerecord/lib/active_record/associations.rb
@@ -1310,28 +1310,27 @@ def association_join
" 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 @@ def association_join
]
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
View
7 activerecord/test/associations_join_model_test.rb
@@ -287,6 +287,13 @@ def test_eager_load_has_many_through_has_many
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)
View
6 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 @@ def raise_exception(object)
raise Exception.new("You can't add a post")
end
end
+
+class AuthorAddress < ActiveRecord::Base
+ has_one :author
+end
View
5 activerecord/test/fixtures/db_definitions/schema.rb
@@ -19,5 +19,10 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.