Skip to content

Commit

Permalink
Merge pull request #41427 from dylanahsmith/dry-primary-key-constrain…
Browse files Browse the repository at this point in the history
…ts-hash

activerecord: Extract primary key constraints hash to a method
  • Loading branch information
rafaelfranca committed Feb 12, 2021
2 parents e0f95b4 + 1d3ef1a commit 1acde22
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -687,14 +687,14 @@ def update_columns(attributes)
verify_readonly_attribute(name) || name
end

id_in_database = self.id_in_database
update_constraints = _primary_key_constraints_hash
attributes.each do |k, v|
write_attribute_without_type_cast(k, v)
end

affected_rows = self.class._update_record(
attributes,
@primary_key => id_in_database
update_constraints
)

affected_rows == 1
Expand Down Expand Up @@ -893,6 +893,10 @@ def apply_scoping?(options)
(self.class.default_scopes?(all_queries: true) || self.class.global_current_scope)
end

def _primary_key_constraints_hash
{ @primary_key => id_in_database }
end

# A hook to be overridden by association modules.
def destroy_associations
end
Expand All @@ -902,7 +906,7 @@ def destroy_row
end

def _delete_row
self.class._delete_record(@primary_key => id_in_database)
self.class._delete_record(_primary_key_constraints_hash)
end

def _touch_row(attribute_names, time)
Expand All @@ -918,7 +922,7 @@ def _touch_row(attribute_names, time)
def _update_row(attribute_names, attempted_action = "update")
self.class._update_record(
attributes_with_values(attribute_names),
@primary_key => id_in_database
_primary_key_constraints_hash
)
end

Expand Down

0 comments on commit 1acde22

Please sign in to comment.