Skip to content
Browse files

Added support for eagerly including polymorphic has_one associations.…

… (closes #4525) [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4170 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4d23202 commit a9fb1544b0e7e6412e4cb64702585024e24276bf @technoweenie technoweenie committed Apr 5, 2006
View
8 activerecord/CHANGELOG
@@ -1,5 +1,13 @@
*SVN*
+* Added support for eagerly including polymorphic has_one associations. (closes #4525) [Rick]
+
+ class Post < ActiveRecord::Base
+ has_one :tagging, :as => :taggable
+ end
+
+ Post.find :all, :include => :tagging
+
* Added descriptive error messages for invalid has_many :through associations: going through :has_one or :has_and_belongs_to_many [Rick]
* Added support for going through a polymorphic has_many association: (closes #4401) [Rick]
View
16 activerecord/lib/active_record/associations.rb
@@ -1392,7 +1392,7 @@ def initialize(reflection, join_dependency, parent = nil)
@aliased_prefix = "t#{ join_dependency.joins.size }"
@aliased_table_name = table_name # start with the table name
@parent_table_name = parent.active_record.table_name
-
+
if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son}
join_dependency.table_aliases[aliased_table_name] += 1
end
@@ -1473,12 +1473,16 @@ def association_join
aliased_table_name, "#{reflection.options[:as]}_type",
klass.quote(parent.active_record.base_class.name)
]
-
+ when reflection.macro == :has_one && reflection.options[:as]
+ " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
+ table_name_and_alias,
+ aliased_table_name, "#{reflection.options[:as]}_id",
+ parent.aliased_table_name, parent.primary_key,
+ aliased_table_name, "#{reflection.options[:as]}_type",
+ klass.quote(reflection.active_record.name)
+ ]
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
+ foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias,
aliased_table_name, foreign_key,
View
16 activerecord/test/associations_join_model_test.rb
@@ -208,6 +208,14 @@ def test_include_has_many_through
end
end
+ def test_include_polymorphic_has_one
+ post = Post.find_by_id(posts(:welcome).id, :include => :tagging)
+ tagging = taggings(:welcome_general)
+ assert_no_queries do
+ assert_equal tagging, post.tagging
+ end
+ end
+
def test_include_polymorphic_has_many_through
posts = Post.find(:all, :order => 'posts.id')
posts_with_tags = Post.find(:all, :include => :tags, :order => 'posts.id')
@@ -303,6 +311,14 @@ def test_has_many_through_polymorphic_has_many
assert_equal [taggings(:welcome_general), taggings(:thinking_general)], authors(:david).taggings.uniq.sort_by { |t| t.id }
end
+ def test_has_many_through_polymorphic_has_many_with_eager_loading
+ author = Author.find_by_id(authors(:david).id, :include => :taggings)
+ expected_taggings = [taggings(:welcome_general), taggings(:thinking_general)]
+ assert_no_queries do
+ assert_equal expected_taggings, author.taggings.uniq.sort_by { |t| t.id }
+ end
+ end
+
def test_has_many_through_has_many_through
assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).tags }
end
View
4 activerecord/test/fixtures/tag.rb
@@ -1,5 +1,5 @@
class Tag < ActiveRecord::Base
- has_many :taggings, :as => :taggable
+ has_many :taggings
has_many :taggables, :through => :taggings
- has_one :tagging, :as => :taggable
+ has_one :tagging
end

0 comments on commit a9fb154

Please sign in to comment.
Something went wrong with that request. Please try again.