Browse files

Fix eager loading of polymorphic has_one associations nested-included…

… under polymorphic belongs_to associations. [#3233 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent a9c69f3 commit 844da12ba6acfb71a691ba32901d7acf4cbe37b4 @maxim maxim committed with josevalim May 31, 2010
View
8 activerecord/lib/active_record/association_preload.rb
@@ -357,7 +357,13 @@ def find_associated_records(ids, reflection, preload_options)
table_name = reflection.klass.quoted_table_name
if interface = reflection.options[:as]
- conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} #{in_or_equals_for_ids(ids)} and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.sti_name}'"
+ parent_type = if reflection.active_record.abstract_class?
+ self.base_class.sti_name
+ else
+ reflection.active_record.sti_name
+ end
+
+ conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} #{in_or_equals_for_ids(ids)} and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{parent_type}'"
else
foreign_key = reflection.primary_key_name
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}"
View
19 activerecord/test/cases/associations/eager_load_nested_polymorphic_include.rb
@@ -0,0 +1,19 @@
+require 'cases/helper'
+require 'models/tee'
+require 'models/tie'
+require 'models/polymorphic_design'
+require 'models/polymorphic_price'
+
+class EagerLoadNestedPolymorphicIncludeTest < ActiveRecord::TestCase
+ fixtures :tees, :ties, :polymorphic_designs, :polymorphic_prices
+
+ def test_eager_load_polymorphic_has_one_nested_under_polymorphic_belongs_to
+ designs = PolymorphicDesign.scoped(:include => {:designable => :polymorphic_price})
+
+ associated_price_ids = designs.map{|design| design.designable.polymorphic_price.id}
+ expected_price_ids = [1, 2, 3, 4]
+
+ assert expected_price_ids.all?{|expected_id| associated_price_ids.include?(expected_id)},
+ "Expected associated prices to be #{expected_price_ids.inspect} but they were #{associated_price_ids.sort.inspect}"
+ end
+end
View
19 activerecord/test/fixtures/polymorphic_designs.yml
@@ -0,0 +1,19 @@
+awesome_tee_design:
+ id: 1
+ designable_id: 1
+ designable_type: Tee
+
+sucky_tee_design:
+ id: 2
+ designable_id: 2
+ designable_type: Tee
+
+awesome_hat_design:
+ id: 3
+ designable_id: 1
+ designable_type: Tie
+
+sucky_hat_design:
+ id: 4
+ designable_id: 2
+ designable_type: Tie
View
19 activerecord/test/fixtures/polymorphic_prices.yml
@@ -0,0 +1,19 @@
+awesome_tee_price:
+ id: 1
+ sellable_id: 1
+ sellable_type: Tee
+
+sucky_tee_price:
+ id: 2
+ sellable_id: 2
+ sellable_type: Tee
+
+awesome_hat_price:
+ id: 3
+ sellable_id: 1
+ sellable_type: Tie
+
+sucky_hat_price:
+ id: 4
+ sellable_id: 2
+ sellable_type: Tie
View
4 activerecord/test/fixtures/tees.yml
@@ -0,0 +1,4 @@
+awesome_tee:
+ id: 1
+sucky_tee:
+ id: 2
View
4 activerecord/test/fixtures/ties.yml
@@ -0,0 +1,4 @@
+awesome_tie:
+ id: 1
+sucky_tie:
+ id: 2
View
3 activerecord/test/models/polymorphic_design.rb
@@ -0,0 +1,3 @@
+class PolymorphicDesign < ActiveRecord::Base
+ belongs_to :designable, :polymorphic => true
+end
View
3 activerecord/test/models/polymorphic_price.rb
@@ -0,0 +1,3 @@
+class PolymorphicPrice < ActiveRecord::Base
+ belongs_to :sellable, :polymorphic => true
+end
View
4 activerecord/test/models/tee.rb
@@ -0,0 +1,4 @@
+class Tee < ActiveRecord::Base
+ has_one :polymorphic_design, :as => :designable
+ has_one :polymorphic_price, :as => :sellable
+end
View
4 activerecord/test/models/tie.rb
@@ -0,0 +1,4 @@
+class Tie < ActiveRecord::Base
+ has_one :polymorphic_design, :as => :designable
+ has_one :polymorphic_price, :as => :sellable
+end
View
14 activerecord/test/schema/schema.rb
@@ -367,6 +367,16 @@ def create_table(*args, &block)
t.column :updated_on, :datetime
end
+ create_table :polymorphic_designs, :force => true do |t|
+ t.integer :designable_id
+ t.string :designable_type
+ end
+
+ create_table :polymorphic_prices, :force => true do |t|
+ t.integer :sellable_id
+ t.string :sellable_type
+ end
+
create_table :posts, :force => true do |t|
t.integer :author_id
t.string :title, :null => false
@@ -435,6 +445,8 @@ def create_table(*args, &block)
t.datetime :ending
end
+ create_table :ties, :force => true
+
create_table :topics, :force => true do |t|
t.string :title
t.string :author_name
@@ -462,6 +474,8 @@ def create_table(*args, &block)
t.column :taggings_count, :integer, :default => 0
end
+ create_table :tees, :force => true
+
create_table :toys, :primary_key => :toy_id ,:force => true do |t|
t.string :name
t.integer :pet_id, :integer

0 comments on commit 844da12

Please sign in to comment.