mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 20:41:24 +08:00
DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)
The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method. There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
This commit is contained in:
@ -39,9 +39,7 @@ class ApplicationRequest < ActiveRecord::Base
|
||||
def self.req_id(date, req_type, retries = 0)
|
||||
req_type_id = req_types[req_type]
|
||||
|
||||
# a poor man's upsert
|
||||
id = where(date: date, req_type: req_type_id).pluck_first(:id)
|
||||
id ||= create!(date: date, req_type: req_type_id, count: 0).id
|
||||
create_or_find_by!(date: date, req_type: req_type_id).id
|
||||
rescue StandardError # primary key violation
|
||||
if retries == 0
|
||||
req_id(date, req_type, 1)
|
||||
|
Reference in New Issue
Block a user