Sha256: 1bac609fce82a42df5f78e4274b0af93fde6adbcfd37d4a1760c292cca73cc2e

Contents?: true

Size: 1.21 KB

Versions: 41

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module ActiveRecord::Associations
  module ForeignAssociation # :nodoc:
    def foreign_key_present?
      if reflection.klass.primary_key
        owner.attribute_present?(reflection.active_record_primary_key)
      else
        false
      end
    end

    def nullified_owner_attributes
      Hash.new.tap do |attrs|
        Array(reflection.foreign_key).each { |foreign_key| attrs[foreign_key] = nil }
        attrs[reflection.type] = nil if reflection.type.present?
      end
    end

    private
      # Sets the owner attributes on the given record
      def set_owner_attributes(record)
        return if options[:through]

        primary_key_attribute_names = Array(reflection.join_primary_key)
        foreign_key_attribute_names = Array(reflection.join_foreign_key)

        primary_key_foreign_key_pairs = primary_key_attribute_names.zip(foreign_key_attribute_names)

        primary_key_foreign_key_pairs.each do |primary_key, foreign_key|
          value = owner._read_attribute(foreign_key)
          record._write_attribute(primary_key, value)
        end

        if reflection.type
          record._write_attribute(reflection.type, owner.class.polymorphic_name)
        end
      end
  end
end

Version data entries

41 entries across 41 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/associations/foreign_association.rb
activerecord-8.0.1 lib/active_record/associations/foreign_association.rb
activerecord-8.0.0.1 lib/active_record/associations/foreign_association.rb
activerecord-7.2.2.1 lib/active_record/associations/foreign_association.rb
activerecord-7.1.5.1 lib/active_record/associations/foreign_association.rb
activerecord-8.0.0 lib/active_record/associations/foreign_association.rb
activerecord-7.2.2 lib/active_record/associations/foreign_association.rb
activerecord-7.1.5 lib/active_record/associations/foreign_association.rb
activerecord-8.0.0.rc2 lib/active_record/associations/foreign_association.rb
activerecord-7.2.1.2 lib/active_record/associations/foreign_association.rb
activerecord-7.1.4.2 lib/active_record/associations/foreign_association.rb
activerecord-8.0.0.rc1 lib/active_record/associations/foreign_association.rb
activerecord-7.2.1.1 lib/active_record/associations/foreign_association.rb
activerecord-7.1.4.1 lib/active_record/associations/foreign_association.rb
activerecord-8.0.0.beta1 lib/active_record/associations/foreign_association.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/associations/foreign_association.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/associations/foreign_association.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/associations/foreign_association.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/associations/foreign_association.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/associations/foreign_association.rb