diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 15:41:31 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 15:42:34 +0100 |
commit | 3b87c38d029c1626161a3e7699d40da3e789d7cb (patch) | |
tree | b056af6ff031bf5ee83e9d8a25307ae72668d97a /activerecord/lib/active_record/associations | |
parent | 5c0e9333c0973123fb1ce2cde68a1c211d422f91 (diff) | |
download | rails-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/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 6 |
2 files changed, 11 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 9e6d9e73c5..a0efdbc2a4 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -68,7 +68,12 @@ module ActiveRecord end if reflection.source_macro == :belongs_to - key = reflection.association_primary_key + if reflection.options[:polymorphic] + key = reflection.association_primary_key(klass) + else + key = reflection.association_primary_key + end + foreign_key = reflection.foreign_key else key = reflection.foreign_key diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 58c9648ce8..97f531d064 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -45,7 +45,11 @@ module ActiveRecord end def replace_keys(record) - owner[reflection.foreign_key] = record && record[reflection.association_primary_key] + if record + owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)] + else + owner[reflection.foreign_key] = nil + end end def foreign_key_present? |