提交 b1ea2763 编写于 作者: J Jeremy Kemper

Fix has_many :through a polymorphic has_many. Closes #10529 [Aleksey Kondratenko]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 9d79e066
......@@ -212,7 +212,7 @@ def construct_select(custom_select = nil)
def construct_joins(custom_joins = nil)
polymorphic_join = nil
if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to
if @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
......
......@@ -16,6 +16,10 @@
require 'models/tagging'
require 'models/person'
require 'models/reader'
require 'models/parrot'
require 'models/pirate'
require 'models/treasure'
require 'models/price_estimate'
class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
......@@ -1607,7 +1611,8 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
:parrots, :pirates, :treasures, :price_estimates
def test_has_and_belongs_to_many
david = Developer.find(1)
......@@ -2143,6 +2148,10 @@ def test_scoped_find_on_through_association_doesnt_return_read_only_records
tag.save!
end
end
def test_has_many_through_polymorphic_has_manys_works
assert_equal [10, 20].to_set, pirates(:redbeard).treasure_estimates.map(&:price).to_set
end
end
......
saphire_1:
price: 10
estimate_of: sapphire (Treasure)
sapphire_2:
price: 20
estimate_of: sapphire (Treasure)
class Pirate < ActiveRecord::Base
belongs_to :parrot
has_and_belongs_to_many :parrots
has_many :loots, :as => :looter
has_many :treasures, :as => :looter
has_many :treasure_estimates, :through => :treasures, :source => :price_estimates
end
class PriceEstimate < ActiveRecord::Base
belongs_to :estimate_of, :polymorphic => true
end
class Treasure < ActiveRecord::Base
has_and_belongs_to_many :parrots
belongs_to :looter, :polymorphic => true
has_many :price_estimates, :as => :estimate_of
end
......@@ -367,4 +367,10 @@ def create_table(*args, &block)
t.string :name
t.integer :owner_id, :integer
end
create_table :price_estimates, :force => true do |t|
t.string :estimate_of_type
t.integer :estimate_of_id
t.integer :price
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册