aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-02-02 03:27:31 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-02-02 03:27:31 +0000
commitb1ea27630df0f0639900b321d756091b42ed29f7 (patch)
tree4372f5dc041745269ebba322ba6dd902b43792f6
parent9d79e0668d6a51573170ee568c5bac6717ec1a0c (diff)
downloadrails-b1ea27630df0f0639900b321d756091b42ed29f7.tar.gz
rails-b1ea27630df0f0639900b321d756091b42ed29f7.tar.bz2
rails-b1ea27630df0f0639900b321d756091b42ed29f7.zip
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
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb2
-rwxr-xr-xactiverecord/test/cases/associations_test.rb11
-rw-r--r--activerecord/test/fixtures/price_estimates.yml7
-rw-r--r--activerecord/test/models/pirate.rb4
-rw-r--r--activerecord/test/models/price_estimate.rb3
-rw-r--r--activerecord/test/models/treasure.rb2
-rw-r--r--activerecord/test/schema/schema.rb6
7 files changed, 32 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 96d0b73da2..06ea5d1d1c 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -212,7 +212,7 @@ module ActiveRecord
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]
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 0df508d01d..cfc58ffa10 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -16,6 +16,10 @@ require 'models/tag'
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 @@ end
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 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
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
diff --git a/activerecord/test/fixtures/price_estimates.yml b/activerecord/test/fixtures/price_estimates.yml
new file mode 100644
index 0000000000..1149ab17a2
--- /dev/null
+++ b/activerecord/test/fixtures/price_estimates.yml
@@ -0,0 +1,7 @@
+saphire_1:
+ price: 10
+ estimate_of: sapphire (Treasure)
+
+sapphire_2:
+ price: 20
+ estimate_of: sapphire (Treasure)
diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb
index f7193dfbe0..bb4d02c10f 100644
--- a/activerecord/test/models/pirate.rb
+++ b/activerecord/test/models/pirate.rb
@@ -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
diff --git a/activerecord/test/models/price_estimate.rb b/activerecord/test/models/price_estimate.rb
new file mode 100644
index 0000000000..ef3bba36a9
--- /dev/null
+++ b/activerecord/test/models/price_estimate.rb
@@ -0,0 +1,3 @@
+class PriceEstimate < ActiveRecord::Base
+ belongs_to :estimate_of, :polymorphic => true
+end
diff --git a/activerecord/test/models/treasure.rb b/activerecord/test/models/treasure.rb
index 7a429e2a2f..97c690c110 100644
--- a/activerecord/test/models/treasure.rb
+++ b/activerecord/test/models/treasure.rb
@@ -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
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 8ab2022f8b..951217b198 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -367,4 +367,10 @@ ActiveRecord::Schema.define do
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