mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 07:37:55 +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:
@ -58,7 +58,7 @@ module ImportScripts
|
||||
|
||||
def find_username_by_import_id(import_id)
|
||||
user_id = user_id_from_imported_user_id(import_id)
|
||||
User.where(id: user_id).pluck_first(:username) if user_id.present?
|
||||
User.where(id: user_id).pick(:username) if user_id.present?
|
||||
end
|
||||
|
||||
# Get the Discourse Category id based on the id of the source category
|
||||
|
@ -14,7 +14,7 @@ Benchmark.ips do |b|
|
||||
|
||||
b.report("pluck with limit") { User.limit(1).pluck(:name).first }
|
||||
|
||||
b.report("pluck with pluck_first") { User.pluck_first(:name) }
|
||||
b.report("pluck with pick") { User.pick(:name) }
|
||||
|
||||
b.report("raw") { conn.exec("SELECT name FROM users LIMIT 1").getvalue(0, 0) }
|
||||
end
|
||||
|
Reference in New Issue
Block a user