Skip to content

Commit

Permalink
Move set_owner_attributes into ForeignAssociation
Browse files Browse the repository at this point in the history
It is only used for foreign associations.
  • Loading branch information
kamipo committed Jun 20, 2020
1 parent 4148590 commit fa2628a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
19 changes: 0 additions & 19 deletions activerecord/lib/active_record/associations/association.rb
Expand Up @@ -249,25 +249,6 @@ def find_target?
!loaded? && (!owner.new_record? || foreign_key_present?) && klass
end

def creation_attributes
attributes = {}

if (reflection.has_one? || reflection.collection?) && !options[:through]
attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]

if reflection.type
attributes[reflection.type] = owner.class.polymorphic_name
end
end

attributes
end

# Sets the owner attributes on the given record
def set_owner_attributes(record)
creation_attributes.each { |key, value| record[key] = value }
end

# Returns true if there is a foreign key present on the owner which
# references the target. This is used to determine whether we can load
# the target if the owner is currently a new record (and therefore
Expand Down
13 changes: 13 additions & 0 deletions activerecord/lib/active_record/associations/foreign_association.rb
Expand Up @@ -16,5 +16,18 @@ def nullified_owner_attributes
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]

key = owner._read_attribute(reflection.join_foreign_key)
record._write_attribute(reflection.join_primary_key, key)

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

0 comments on commit fa2628a

Please sign in to comment.