diff options
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.rb | 13 |
1 files changed, 13 insertions, 0 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 a0df860623..c580de7fbe 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -23,6 +23,19 @@ module ActiveRecord @updated end + def stale_target? + if @target && @target.persisted? + target_id = @target.send(@reflection.association_primary_key).to_s + foreign_key = @owner.send(@reflection.primary_key_name).to_s + target_type = @target.class.base_class.name + foreign_type = @owner.send(@reflection.options[:foreign_type]).to_s + + target_id != foreign_key || target_type != foreign_type + else + false + end + end + private # NOTE - for now, we're only supporting inverse setting from belongs_to back onto |