From 752f633bc55c191b0b8b555e1444d254334c4157 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 4 Jan 2008 23:21:52 +0000 Subject: Document that eager loading doesn't work with polymorphic associations. Closes #10610 [Xavier Noria] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8556 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index deadba7418..a82ea23b84 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -487,6 +487,22 @@ module ActiveRecord # When eager loaded, conditions are interpolated in the context of the model class, not the model instance. Conditions are lazily interpolated # before the actual model exists. # + # Eager loading is not possible with polymorphic associations. Given + # + # class Address < ActiveRecord::Base + # belongs_to :addressable, :polymorphic => true + # end + # + # a call that tries to eager load the addressable model + # + # Address.find(:all, :include => :addressable) # INVALID + # + # will raise ActiveRecord::EagerLoadPolymorphicError. The reason is that the parent model's type + # is a column value so its corresponding table name cannot be put in the FROM/JOIN clauses of that early query. + # + # It does work the other way around though: if the User model is addressable you can eager load + # their addresses with :include just fine, every piece needed to construct the query is known beforehand. + # # == Table Aliasing # # ActiveRecord uses table aliasing in the case that a table is referenced multiple times in a join. If a table is referenced only once, @@ -783,6 +799,7 @@ module ActiveRecord # a column name instead of a +true+/+false+ value to this option (e.g., :counter_cache => :my_custom_counter.) # Note: Specifying a counter_cache will add it to that model's list of readonly attributes using #attr_readonly. # * :include - specify second-order associations that should be eager loaded when this object is loaded. + # Not allowed if the association is polymorphic. # * :polymorphic - specify this association is a polymorphic association by passing +true+. # Note: If you've enabled the counter cache, then you may want to add the counter cache attribute # to the attr_readonly list in the associated classes (e.g. class Post; attr_readonly :comments_count; end). -- cgit v1.2.3