Permalink
Browse files

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
  • Loading branch information...
jeremy committed Feb 2, 2008
1 parent 9d79e06 commit b1ea27630df0f0639900b321d756091b42ed29f7
@@ -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
@@ -0,0 +1,7 @@
+saphire_1:
+ price: 10
+ estimate_of: sapphire (Treasure)
+
+sapphire_2:
+ price: 20
+ estimate_of: sapphire (Treasure)
@@ -1,5 +1,7 @@
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
@@ -0,0 +1,3 @@
+class PriceEstimate < ActiveRecord::Base
+ belongs_to :estimate_of, :polymorphic => true
+end
@@ -1,4 +1,6 @@
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

0 comments on commit b1ea276

Please sign in to comment.