aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-05-16 17:07:21 +0100
committerJon Leighton <j@jonathanleighton.com>2011-05-22 21:25:02 +0100
commitd7a910e31e839e5544aa9f83f9922ecfcee74ed5 (patch)
treee27b725c1805246b2dd07aabec5fda5caf8d259e
parent8c19ebe5539fae896a1a6fb2549e9e63445b6607 (diff)
downloadrails-d7a910e31e839e5544aa9f83f9922ecfcee74ed5.tar.gz
rails-d7a910e31e839e5544aa9f83f9922ecfcee74ed5.tar.bz2
rails-d7a910e31e839e5544aa9f83f9922ecfcee74ed5.zip
Fix problem with loading polymorphic associations which have been defined in an abstract superclass. Fixes #552.
-rw-r--r--activerecord/lib/active_record/associations/association_scope.rb20
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb10
-rw-r--r--activerecord/lib/active_record/reflection.rb9
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb7
-rw-r--r--activerecord/test/cases/reflection_test.rb4
5 files changed, 35 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb
index ab102b2b8f..94847bc2ae 100644
--- a/activerecord/lib/active_record/associations/association_scope.rb
+++ b/activerecord/lib/active_record/associations/association_scope.rb
@@ -75,10 +75,16 @@ module ActiveRecord
foreign_key = reflection.active_record_primary_key
end
+ conditions = self.conditions[i]
+
if reflection == chain.last
scope = scope.where(table[key].eq(owner[foreign_key]))
- conditions[i].each do |condition|
+ if reflection.type
+ scope = scope.where(table[reflection.type].eq(owner.class.base_class.name))
+ end
+
+ conditions.each do |condition|
if options[:through] && condition.is_a?(Hash)
condition = { table.name => condition }
end
@@ -87,12 +93,16 @@ module ActiveRecord
end
else
constraint = table[key].eq(foreign_table[foreign_key])
- join = join(foreign_table, constraint)
- scope = scope.joins(join)
+ if reflection.type
+ type = chain[i + 1].klass.base_class.name
+ constraint = constraint.and(table[reflection.type].eq(type))
+ end
+
+ scope = scope.joins(join(foreign_table, constraint))
- unless conditions[i].empty?
- scope = scope.where(sanitize(conditions[i], table))
+ unless conditions.empty?
+ scope = scope.where(sanitize(conditions, table))
end
end
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
index c32753782f..03963ab060 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -62,6 +62,7 @@ module ActiveRecord
def join_to(relation)
tables = @tables.dup
foreign_table = parent_table
+ foreign_klass = parent.active_record
# The chain starts with the target table, but we want to end with it here (makes
# more sense in this context), so we reverse
@@ -91,14 +92,17 @@ module ActiveRecord
constraint = build_constraint(reflection, table, key, foreign_table, foreign_key)
- unless conditions[i].empty?
- constraint = constraint.and(sanitize(conditions[i], table))
+ conditions = self.conditions[i].dup
+ conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
+
+ unless conditions.empty?
+ constraint = constraint.and(sanitize(conditions, table))
end
relation.from(join(table, constraint))
# The current table in this iteration becomes the foreign table in the next
- foreign_table = table
+ foreign_table, foreign_klass = table, reflection.klass
end
relation
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index bcba85d7a4..b1bb1b0f7f 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -212,7 +212,7 @@ module ActiveRecord
end
def type
- @type ||= "#{options[:as]}_type"
+ @type ||= options[:as] && "#{options[:as]}_type"
end
def primary_key_column
@@ -280,9 +280,7 @@ module ActiveRecord
# in the #chain. The inside arrays are simply conditions (and each condition may itself be
# a hash, array, arel predicate, etc...)
def conditions
- conditions = [options[:conditions]].compact
- conditions << { type => active_record.base_class.name } if options[:as]
- [conditions]
+ [[options[:conditions]].compact]
end
alias :source_macro :macro
@@ -378,7 +376,8 @@ module ActiveRecord
# Holds all the meta-data about a :through association as it was specified
# in the Active Record class.
class ThroughReflection < AssociationReflection #:nodoc:
- delegate :foreign_key, :foreign_type, :association_foreign_key, :active_record_primary_key, :to => :source_reflection
+ delegate :foreign_key, :foreign_type, :association_foreign_key,
+ :active_record_primary_key, :type, :to => :source_reflection
# Gets the source of the through reflection. It checks both a singularized
# and pluralized form for <tt>:belongs_to</tt> or <tt>:has_many</tt>.
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 522ac56d82..43974fd895 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -11,6 +11,7 @@ require 'models/comment'
require 'models/person'
require 'models/reader'
require 'models/tagging'
+require 'models/tag'
require 'models/invoice'
require 'models/line_item'
require 'models/car'
@@ -1468,4 +1469,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
bulb = car.bulbs.build({ :bulb_type => :custom }, :as => :admin)
assert_equal CustomBulb, bulb.class
end
+
+ def test_abstract_class_with_polymorphic_has_many
+ post = SubStiPost.create! :title => "fooo", :body => "baa"
+ tagging = Tagging.create! :taggable => post
+ assert_equal [tagging], post.taggings
+ end
end
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb
index 97d9669483..7e4ae1ea8d 100644
--- a/activerecord/test/cases/reflection_test.rb
+++ b/activerecord/test/cases/reflection_test.rb
@@ -216,7 +216,7 @@ class ReflectionTest < ActiveRecord::TestCase
def test_conditions
expected = [
[{ :tags => { :name => 'Blue' } }],
- [{ :taggings => { :comment => 'first' } }, { "taggable_type" => "Post" }],
+ [{ :taggings => { :comment => 'first' } }],
[{ :posts => { :title => ['misc post by bob', 'misc post by mary'] } }]
]
actual = Author.reflect_on_association(:misc_post_first_blue_tags).conditions
@@ -224,7 +224,7 @@ class ReflectionTest < ActiveRecord::TestCase
expected = [
[{ :tags => { :name => 'Blue' } }, { :taggings => { :comment => 'first' } }, { :posts => { :title => ['misc post by bob', 'misc post by mary'] } }],
- [{ "taggable_type" => "Post" }],
+ [],
[]
]
actual = Author.reflect_on_association(:misc_post_first_blue_tags_2).conditions