Class: ActiveRecord::Associations::Builder::BelongsTo

Inherits:
SingularAssociation show all
Defined in:
activerecord/lib/active_record/associations/builder/belongs_to.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Association

Association::VALID_OPTIONS

Class Method Summary collapse

Methods inherited from Association

build, create_reflection

Class Method Details

.touch_record(o, changes, foreign_key, name, touch, touch_method) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'activerecord/lib/active_record/associations/builder/belongs_to.rb', line 42

def self.touch_record(o, changes, foreign_key, name, touch, touch_method) # :nodoc:
  old_foreign_id = changes[foreign_key] && changes[foreign_key].first

  if old_foreign_id
    association = o.association(name)
    reflection = association.reflection
    if reflection.polymorphic?
      foreign_type = reflection.foreign_type
      klass = changes[foreign_type] && changes[foreign_type].first || o.public_send(foreign_type)
      klass = klass.constantize
    else
      klass = association.klass
    end
    primary_key = reflection.association_primary_key(klass)
    old_record = klass.find_by(primary_key => old_foreign_id)

    if old_record
      if touch != true
        old_record.send(touch_method, touch)
      else
        old_record.send(touch_method)
      end
    end
  end

  record = o.send name
  if record && record.persisted?
    if touch != true
      record.send(touch_method, touch)
    else
      record.send(touch_method)
    end
  end
end