aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/reflection.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-09-26 15:41:31 +0100
committerJon Leighton <j@jonathanleighton.com>2011-09-26 15:42:34 +0100
commit3b87c38d029c1626161a3e7699d40da3e789d7cb (patch)
treeb056af6ff031bf5ee83e9d8a25307ae72668d97a /activerecord/lib/active_record/reflection.rb
parent5c0e9333c0973123fb1ce2cde68a1c211d422f91 (diff)
downloadrails-3b87c38d029c1626161a3e7699d40da3e789d7cb.tar.gz
rails-3b87c38d029c1626161a3e7699d40da3e789d7cb.tar.bz2
rails-3b87c38d029c1626161a3e7699d40da3e789d7cb.zip
Fix belongs_to polymorphic with custom primary key on target.
Closes #3104.
Diffstat (limited to 'activerecord/lib/active_record/reflection.rb')
-rw-r--r--activerecord/lib/active_record/reflection.rb26
1 files changed, 11 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 89179779e3..1929a808ed 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -211,11 +211,9 @@ module ActiveRecord
@association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end
- def association_primary_key
- @association_primary_key ||=
- options[:primary_key] ||
- !options[:polymorphic] && klass.primary_key ||
- 'id'
+ # klass option is necessary to support loading polymorphic associations
+ def association_primary_key(klass = self.klass)
+ options[:primary_key] || klass.primary_key
end
def active_record_primary_key
@@ -463,17 +461,15 @@ module ActiveRecord
# We want to use the klass from this reflection, rather than just delegate straight to
# the source_reflection, because the source_reflection may be polymorphic. We still
# need to respect the source_reflection's :primary_key option, though.
- def association_primary_key
- @association_primary_key ||= begin
- # Get the "actual" source reflection if the immediate source reflection has a
- # source reflection itself
- source_reflection = self.source_reflection
- while source_reflection.source_reflection
- source_reflection = source_reflection.source_reflection
- end
-
- source_reflection.options[:primary_key] || klass.primary_key
+ def association_primary_key(klass = self.klass)
+ # Get the "actual" source reflection if the immediate source reflection has a
+ # source reflection itself
+ source_reflection = self.source_reflection
+ while source_reflection.source_reflection
+ source_reflection = source_reflection.source_reflection
end
+
+ source_reflection.options[:primary_key] || klass.primary_key
end
# Gets an array of possible <tt>:through</tt> source reflection names: