aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-02 14:28:53 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-03 16:24:31 -0800
commita0be389d39b790e0625339251d2674b8250b16b1 (patch)
tree256369916a5916d03d2b9e614582b0f7a9c35d74 /activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
parentc47f802d0e7b0156512f197887d6e9bda6d0f269 (diff)
downloadrails-a0be389d39b790e0625339251d2674b8250b16b1.tar.gz
rails-a0be389d39b790e0625339251d2674b8250b16b1.tar.bz2
rails-a0be389d39b790e0625339251d2674b8250b16b1.zip
Allow assignment on has_one :through where the owner is a new record [#5137 state:resolved]
This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct. Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things. Phew, long commit message!
Diffstat (limited to 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb17
1 files changed, 4 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
index 4608ffad67..4f67b02d00 100644
--- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -2,19 +2,6 @@ module ActiveRecord
# = Active Record Belongs To Polymorphic Association
module Associations
class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
- def stale_target?
- if @target && @target.persisted?
- target_id = @target.send(@reflection.association_primary_key).to_s
- foreign_key = @owner.send(@reflection.foreign_key).to_s
- target_type = @target.class.base_class.name
- foreign_type = @owner.send(@reflection.foreign_type).to_s
-
- target_id != foreign_key || target_type != foreign_type
- else
- false
- end
- end
-
private
def replace_keys(record)
@@ -38,6 +25,10 @@ module ActiveRecord
def raise_on_type_mismatch(record)
# A polymorphic association cannot have a type mismatch, by definition
end
+
+ def stale_state
+ [super, @owner[@reflection.foreign_type].to_s]
+ end
end
end
end