提交 844da12b 编写于 作者: M Maxim Chernyak 提交者: José Valim

Fix eager loading of polymorphic has_one associations nested-included under...

Fix eager loading of polymorphic has_one associations nested-included under polymorphic belongs_to associations. [#3233 state:resolved]
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 a9c69f3b
......@@ -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)}"
......
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
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
\ No newline at end of file
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
\ No newline at end of file
awesome_tee:
id: 1
sucky_tee:
id: 2
\ No newline at end of file
awesome_tie:
id: 1
sucky_tie:
id: 2
\ No newline at end of file
class PolymorphicDesign < ActiveRecord::Base
belongs_to :designable, :polymorphic => true
end
\ No newline at end of file
class PolymorphicPrice < ActiveRecord::Base
belongs_to :sellable, :polymorphic => true
end
\ No newline at end of file
class Tee < ActiveRecord::Base
has_one :polymorphic_design, :as => :designable
has_one :polymorphic_price, :as => :sellable
end
\ No newline at end of file
class Tie < ActiveRecord::Base
has_one :polymorphic_design, :as => :designable
has_one :polymorphic_price, :as => :sellable
end
\ No newline at end of file
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册