A platform for community discussion. Free, open, simple.
DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)
Ted Johansson25a226279a54
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex a7293b65828..2212a9752d4 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -365,7 +365,7 @@ class CategoriesController < ApplicationControllerif SiteSetting.enable_category_group_moderation?params[:reviewable_by_group_id] = Group.where(name: params[:reviewable_by_group_name],- ).pluck_first(:id) if params[:reviewable_by_group_name]+ ).pick(:id) if params[:reviewable_by_group_name]endresult =diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rbindex 15f1451d5a8..33bac7ec275 100644--- a/app/controllers/directory_items_controller.rb+++ b/app/controllers/directory_items_controller.rb@@ -70,7 +70,7 @@ class DirectoryItemsController < ApplicationControllerendif params[:username]- user_id = User.where(username_lower: params[:username].to_s.downcase).pluck_first(:id)+ user_id = User.where(username_lower: params[:username].to_s.downcase).pick(:id)if user_idresult = result.where(user_id: user_id)elsediff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex 6e34b868676..b9e827190f8 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -456,7 +456,7 @@ class ListController < ApplicationControllerdef self.best_period_for(previous_visit_at, category_id = nil)default_period =(- (category_id && Category.where(id: category_id).pluck_first(:default_top_period)) ||+ (category_id && Category.where(id: category_id).pick(:default_top_period)) ||SiteSetting.top_page_default_timeframe).to_symdiff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex 2b2a329e2b5..e311f7ac5ba 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -615,7 +615,7 @@ class PostsController < ApplicationControllerbookmarkable_id: params[:post_id],bookmarkable_type: "Post",user_id: current_user.id,- ).pluck_first(:id)+ ).pick(:id)destroyed_bookmark = BookmarkManager.new(current_user).destroy(bookmark_id)render json:diff --git a/app/controllers/stylesheets_controller.rb b/app/controllers/stylesheets_controller.rbindex 97edb80b576..1d6c77f0a65 100644--- a/app/controllers/stylesheets_controller.rb+++ b/app/controllers/stylesheets_controller.rb@@ -59,7 +59,7 @@ class StylesheetsController < ApplicationControllercache_path = Stylesheet::Manager.cache_fullpathlocation = "#{cache_path}/#{target}#{underscore_digest}#{extension}"- stylesheet_time = query.pluck_first(:created_at)+ stylesheet_time = query.pick(:created_at)handle_missing_cache(location, target, digest) if !stylesheet_time@@ -68,7 +68,7 @@ class StylesheetsController < ApplicationControllerendunless File.exist?(location)- if current = query.pluck_first(source_map ? :source_map : :content)+ if current = query.pick(source_map ? :source_map : :content)FileUtils.mkdir_p(cache_path)File.write(location, current)elsediff --git a/app/controllers/theme_javascripts_controller.rb b/app/controllers/theme_javascripts_controller.rbindex 159402889fe..f311ebfe391 100644--- a/app/controllers/theme_javascripts_controller.rb+++ b/app/controllers/theme_javascripts_controller.rb@@ -22,7 +22,7 @@ class ThemeJavascriptsController < ApplicationControllercache_file = "#{DISK_CACHE_PATH}/#{params[:digest]}.js"write_if_not_cached(cache_file) do- content, has_source_map = query.pluck_first(:content, "source_map IS NOT NULL")+ content, has_source_map = query.pick(:content, "source_map IS NOT NULL")if has_source_mapcontent +="\n//# sourceMappingURL=#{params[:digest]}.map?__ws=#{Discourse.current_hostname}\n"@@ -40,7 +40,7 @@ class ThemeJavascriptsController < ApplicationController# Security: safe due to route constraintcache_file = "#{DISK_CACHE_PATH}/#{params[:digest]}.map"- write_if_not_cached(cache_file) { query.pluck_first(:source_map) }+ write_if_not_cached(cache_file) { query.pick(:source_map) }serve_file(cache_file)end@@ -75,7 +75,7 @@ class ThemeJavascriptsController < ApplicationControllerif params[:action].to_s == "show_tests"File.exist?(@cache_file) ? File.ctime(@cache_file) : nilelse- query.pluck_first(:updated_at)+ query.pick(:updated_at)endendenddiff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 27ac78fc52e..612827147e4 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -843,7 +843,7 @@ class TopicsController < ApplicationControllerif params[:title].present?# when creating a new topic, ensure the 1st post is a regular post- if Post.where(topic: topic, id: post_ids).order(:post_number).pluck_first(:post_type) !=+ if Post.where(topic: topic, id: post_ids).order(:post_number).pick(:post_type) !=Post.types[:regular]return(render_json_error(diff --git a/app/controllers/user_badges_controller.rb b/app/controllers/user_badges_controller.rbindex 1617f55516d..ac0e78be29a 100644--- a/app/controllers/user_badges_controller.rb+++ b/app/controllers/user_badges_controller.rb@@ -22,7 +22,7 @@ class UserBadgesController < ApplicationControllergrant_count = nilif params[:username]- user_id = User.where(username_lower: params[:username].downcase).pluck_first(:id)+ user_id = User.where(username_lower: params[:username].downcase).pick(:id)user_badges = user_badges.where(user_id: user_id) if user_idgrant_count = badge.user_badges.where(user_id: user_id).countenddiff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 3f7cafff491..cc7cc1bd928 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -190,7 +190,7 @@ module ApplicationHelperresult << "category-#{@category.slug_path.join("-")}" if @category && @category.url.present?if current_user.present? && current_user.primary_group_id &&- primary_group_name = Group.where(id: current_user.primary_group_id).pluck_first(:name)+ primary_group_name = Group.where(id: current_user.primary_group_id).pick(:name)result << "primary-group-#{primary_group_name.downcase}"end@@ -544,7 +544,7 @@ module ApplicationHelperreturn if theme_id.blank?- @scheme_id = Theme.where(id: theme_id).pluck_first(:color_scheme_id)+ @scheme_id = Theme.where(id: theme_id).pick(:color_scheme_id)enddef dark_scheme_iddiff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rbindex 41114ec140c..26c3f64099d 100644--- a/app/jobs/regular/update_username.rb+++ b/app/jobs/regular/update_username.rb@@ -197,9 +197,11 @@ module Jobsenddef quotes_correct_user?(aside)- Post.where(topic_id: aside["data-topic"], post_number: aside["data-post"]).pluck_first(- :user_id,- ) == @user_id+ Post.exists?(+ topic_id: aside["data-topic"],+ post_number: aside["data-post"],+ user_id: @user_id,+ )endendenddiff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex d5580858274..a5545b05bd5 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -504,7 +504,7 @@ class UserNotifications < ActionMailer::Baseuser_name = notification_data[:original_username]if post && SiteSetting.enable_names && SiteSetting.display_name_on_email_from- name = User.where(id: notification_data[:original_user_id] || post.user_id).pluck_first(:name)+ name = User.where(id: notification_data[:original_user_id] || post.user_id).pick(:name)user_name = name unless name.blank?end@@ -589,7 +589,7 @@ class UserNotifications < ActionMailer::Base# subcategory caseif !category.parent_category_id.nil?show_category_in_subject =- "#{Category.where(id: category.parent_category_id).pluck_first(:name)}/#{show_category_in_subject}"+ "#{Category.where(id: category.parent_category_id).pick(:name)}/#{show_category_in_subject}"endelseshow_category_in_subject = nildiff --git a/app/models/application_request.rb b/app/models/application_request.rbindex d422a08c960..bb308a74356 100644--- a/app/models/application_request.rb+++ b/app/models/application_request.rb@@ -39,9 +39,7 @@ class ApplicationRequest < ActiveRecord::Basedef 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).idrescue StandardError # primary key violationif retries == 0req_id(date, req_type, 1)diff --git a/app/models/backup_metadata.rb b/app/models/backup_metadata.rbindex 3fdcd071d31..92ea233ba50 100644--- a/app/models/backup_metadata.rb+++ b/app/models/backup_metadata.rb@@ -4,7 +4,7 @@ class BackupMetadata < ActiveRecord::BaseLAST_RESTORE_DATE = "last_restore_date"def self.value_for(name)- where(name: name).pluck_first(:value).presence+ where(name: name).pick(:value).presenceenddef self.last_restore_datediff --git a/app/models/category.rb b/app/models/category.rbindex 39a2c70bf4f..06be2853336 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -790,9 +790,8 @@ class Category < ActiveRecord::Basedef self.query_parent_category(parent_slug)encoded_parent_slug = CGI.escape(parent_slug) if SiteSetting.slug_generation_method == "encoded"- self.where(slug: (encoded_parent_slug || parent_slug), parent_category_id: nil).pluck_first(- :id,- ) || self.where(id: parent_slug.to_i).pluck_first(:id)+ self.where(slug: (encoded_parent_slug || parent_slug), parent_category_id: nil).pick(:id) ||+ self.where(id: parent_slug.to_i).pick(:id)enddef self.query_category(slug_or_id, parent_category_id)diff --git a/app/models/draft.rb b/app/models/draft.rbindex a9dff7f2774..942ce774826 100644--- a/app/models/draft.rb+++ b/app/models/draft.rb@@ -252,7 +252,7 @@ class Draft < ActiveRecord::Basereply = JSON.parse(data)["reply"] || ""return if reply.length < SiteSetting.backup_drafts_to_pm_length- post_id = BackupDraftPost.where(user_id: user.id, key: key).pluck_first(:post_id)+ post_id = BackupDraftPost.where(user_id: user.id, key: key).pick(:post_id)post = Post.where(id: post_id).first if post_idBackupDraftPost.where(user_id: user.id, key: key).delete_all if post_id && !post@@ -305,7 +305,7 @@ class Draft < ActiveRecord::Baseenddef self.ensure_draft_topic!(user)- topic_id = BackupDraftTopic.where(user_id: user.id).pluck_first(:topic_id)+ topic_id = BackupDraftTopic.where(user_id: user.id).pick(:topic_id)topic = Topic.find_by(id: topic_id) if topic_idBackupDraftTopic.where(user_id: user.id).delete_all if topic_id && !topicdiff --git a/app/models/incoming_link.rb b/app/models/incoming_link.rbindex acb7d0b3955..694ce06e503 100644--- a/app/models/incoming_link.rb+++ b/app/models/incoming_link.rb@@ -37,7 +37,7 @@ class IncomingLink < ActiveRecord::Baseif host != opts[:host] && (user_id || referer)post_id = opts[:post_id]post_id ||=- Post.where(topic_id: opts[:topic_id], post_number: opts[:post_number] || 1).pluck_first(:id)+ Post.where(topic_id: opts[:topic_id], post_number: opts[:post_number] || 1).pick(:id)cid = current_user ? (current_user.id) : (nil)ip_address = nil if ciddiff --git a/app/models/notification.rb b/app/models/notification.rbindex 86ec8057ac5..825a566faf4 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -348,7 +348,7 @@ class Notification < ActiveRecord::Baseenddef post_id- Post.where(topic: topic_id, post_number: post_number).pluck_first(:id)+ Post.where(topic: topic_id, post_number: post_number).pick(:id)endprotecteddiff --git a/app/models/post.rb b/app/models/post.rbindex 736295205cb..5095f463882 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -1149,7 +1149,7 @@ class Post < ActiveRecord::Baseendupload_id = nil- upload_id = Upload.where(sha1: sha1).pluck_first(:id) if sha1.present?+ upload_id = Upload.where(sha1: sha1).pick(:id) if sha1.present?upload_id ||= yield(post, src, path, sha1)if upload_id.blank?diff --git a/app/models/post_action.rb b/app/models/post_action.rbindex a02c8908cf6..d339316367e 100644--- a/app/models/post_action.rb+++ b/app/models/post_action.rb@@ -242,7 +242,7 @@ class PostAction < ActiveRecord::Baseendend- topic_id = Post.with_deleted.where(id: post_id).pluck_first(:topic_id)+ topic_id = Post.with_deleted.where(id: post_id).pick(:topic_id)# topic_userif post_action_type_key == :likediff --git a/app/models/quoted_post.rb b/app/models/quoted_post.rbindex 54d68c9a926..131a9d98aae 100644--- a/app/models/quoted_post.rb+++ b/app/models/quoted_post.rb@@ -68,7 +68,7 @@ class QuotedPost < ActiveRecord::Baseif post.reply_to_post_numberreply_post_id =- Post.where(topic_id: post.topic_id, post_number: post.reply_to_post_number).pluck_first(:id)+ Post.where(topic_id: post.topic_id, post_number: post.reply_to_post_number).pick(:id)reply_quoted =reply_post_id.present? &&QuotedPost.where(post_id: post.id, quoted_post_id: reply_post_id).count > 0diff --git a/app/models/screened_ip_address.rb b/app/models/screened_ip_address.rbindex ed6cc3d800b..136164ecb0a 100644--- a/app/models/screened_ip_address.rb+++ b/app/models/screened_ip_address.rb@@ -147,7 +147,7 @@ class ScreenedIpAddress < ActiveRecord::Base.where("masklen(ip_address) IN (?)", from_masklen)sum_match_count, max_last_match_at, min_created_at =- old_ips.pluck_first("SUM(match_count), MAX(last_match_at), MIN(created_at)")+ old_ips.pick("SUM(match_count), MAX(last_match_at), MIN(created_at)")ScreenedIpAddress.create!(ip_address: subnet,diff --git a/app/models/topic.rb b/app/models/topic.rbindex b1e764b7a97..0be942c4815 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -893,7 +893,7 @@ class Topic < ActiveRecord::Base# If a post is deleted we have to update our highest post counters and last post informationdef self.reset_highest(topic_id)- archetype = Topic.where(id: topic_id).pluck_first(:archetype)+ archetype = Topic.where(id: topic_id).pick(:archetype)# ignore small_action replies for private messagespost_type =diff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rbindex 8eab3a31d0c..3c0b9652a95 100644--- a/app/models/topic_converter.rb+++ b/app/models/topic_converter.rb@@ -20,7 +20,7 @@ class TopicConverter.where(read_restricted: false).where.not(id: SiteSetting.uncategorized_category_id).order("id asc")- .pluck_first(:id)+ .pick(:id)endPostRevisor.new(@topic.first_post, @topic).revise!(diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 8f9bb37c22a..98c171e070f 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -254,9 +254,7 @@ class TopicEmbed < ActiveRecord::Basedef self.topic_id_for_embed(embed_url)embed_url = normalize_url(embed_url).sub(%r{\Ahttps?\://}, "")- TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").pluck_first(- :topic_id,- )+ TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").pick(:topic_id)enddef self.first_paragraph_from(html)@@ -281,7 +279,7 @@ class TopicEmbed < ActiveRecord::BaseDiscourse.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do- url = TopicEmbed.where(topic_id: post.topic_id).pluck_first(:embed_url)+ url = TopicEmbed.where(topic_id: post.topic_id).pick(:embed_url)response = TopicEmbed.find_remote(url)body = response.bodydiff --git a/app/models/topic_user.rb b/app/models/topic_user.rbindex 0e00f68f2de..6b58cfbe464 100644--- a/app/models/topic_user.rb+++ b/app/models/topic_user.rb@@ -275,8 +275,7 @@ class TopicUser < ActiveRecord::Baseattrs[:notification_level] = notification_levels[:watching]endelse- auto_track_after =- UserOption.where(user_id: user_id).pluck_first(:auto_track_topics_after_msecs)+ auto_track_after = UserOption.where(user_id: user_id).pick(:auto_track_topics_after_msecs)auto_track_after ||= SiteSetting.default_other_auto_track_topics_after_msecsif auto_track_after >= 0 && auto_track_after <= (attrs[:total_msecs_viewed].to_i || 0)diff --git a/app/models/user.rb b/app/models/user.rbindex f188ec78f51..92b2c72be26 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -294,7 +294,7 @@ class User < ActiveRecord::Base->(filter) {if filter.is_a?(String) && filter =~ /.+@.+/# probably an email so try the bypass- if user_id = UserEmail.where("lower(email) = ?", filter.downcase).pluck_first(:user_id)+ if user_id = UserEmail.where("lower(email) = ?", filter.downcase).pick(:user_id)return where("users.id = ?", user_id)endend@@ -1683,11 +1683,11 @@ class User < ActiveRecord::Basegroup_titles_query.order("groups.id = #{primary_group_id} DESC") if primary_group_idgroup_titles_query = group_titles_query.order("groups.primary_group DESC").limit(1)- if next_best_group_title = group_titles_query.pluck_first(:title)+ if next_best_group_title = group_titles_query.pick(:title)return next_best_group_titleend- next_best_badge_title = badges.where(allow_title: true).pluck_first(:name)+ next_best_badge_title = badges.where(allow_title: true).pick(:name)next_best_badge_title ? Badge.display_name(next_best_badge_title) : nilend@@ -2030,9 +2030,7 @@ class User < ActiveRecord::Basedef match_primary_group_changesreturn unless primary_group_id_changed?- if title == Group.where(id: primary_group_id_was).pluck_first(:title)- self.title = primary_group&.title- end+ self.title = primary_group&.title if Group.exists?(id: primary_group_id_was, title: title)self.flair_group_id = primary_group&.id if flair_group_id == primary_group_id_wasend@@ -2043,7 +2041,7 @@ class User < ActiveRecord::Base.human_users.joins(:user_auth_tokens).order("user_auth_tokens.created_at")- .pluck_first(:id)+ .pick(:id)endprivatediff --git a/app/models/user_action.rb b/app/models/user_action.rbindex d605324de47..5aa7529fa2e 100644--- a/app/models/user_action.rb+++ b/app/models/user_action.rb@@ -69,7 +69,7 @@ class UserAction < ActiveRecord::BaseUserAction.where(user_id: user_id, target_topic_id: topic_id, action_type: [RESPONSE, MENTION, QUOTE]).order("created_at DESC")- .pluck_first(:target_post_id)+ .pick(:target_post_id)enddef self.stats(user_id, guardian)diff --git a/app/models/web_crawler_request.rb b/app/models/web_crawler_request.rbindex c1660f65555..8be7be49e01 100644--- a/app/models/web_crawler_request.rb+++ b/app/models/web_crawler_request.rb@@ -24,7 +24,7 @@ class WebCrawlerRequest < ActiveRecord::Baseprotecteddef self.request_id(date:, user_agent:, retries: 0)- id = where(date: date, user_agent: user_agent).pluck_first(:id)+ id = where(date: date, user_agent: user_agent).pick(:id)id ||= create!({ date: date, user_agent: user_agent }.merge(count: 0)).idrescue StandardError # primary key violationif retries == 0diff --git a/app/serializers/suggested_topics_mixin.rb b/app/serializers/suggested_topics_mixin.rbindex a899d73f7d1..e257a9436d6 100644--- a/app/serializers/suggested_topics_mixin.rb+++ b/app/serializers/suggested_topics_mixin.rb@@ -33,7 +33,7 @@ module SuggestedTopicsMixinobject.topic_allowed_group_ids,scope.user.id,)- .pluck_first(:name)+ .pick(:name)endenddiff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rbindex 91eb2a6f2f1..d1f66fe93e5 100644--- a/app/serializers/topic_view_serializer.rb+++ b/app/serializers/topic_view_serializer.rb@@ -283,7 +283,7 @@ class TopicViewSerializer < ApplicationSerializerowner: true,},)- .pluck_first(:name)+ .pick(:name)enddef include_requested_group_name?diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex b792a6e701d..db6906157d2 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -385,8 +385,7 @@ class PostAlerterstats = (@group_stats[topic.id] ||= group_stats(topic))return unless stats- group_id =- topic.topic_allowed_groups.where(group_id: user.groups.pluck(:id)).pluck_first(:group_id)+ group_id = topic.topic_allowed_groups.where(group_id: user.groups.pluck(:id)).pick(:group_id)stat = stats.find { |s| s[:group_id] == group_id }return unless statdiff --git a/db/fixtures/600_themes.rb b/db/fixtures/600_themes.rbindex ce01f8eb1ce..f58ddd69cbf 100644--- a/db/fixtures/600_themes.rb+++ b/db/fixtures/600_themes.rb@@ -30,7 +30,7 @@ if !Theme.exists?if SiteSetting.default_dark_mode_color_scheme_id ==SiteSetting.defaults[:default_dark_mode_color_scheme_id]- dark_scheme_id = ColorScheme.where(base_scheme_id: "Dark").pluck_first(:id)+ dark_scheme_id = ColorScheme.where(base_scheme_id: "Dark").pick(:id)SiteSetting.default_dark_mode_color_scheme_id = dark_scheme_id if dark_scheme_id.present?enddiff --git a/lib/composer_messages_finder.rb b/lib/composer_messages_finder.rbindex e394bb39840..b82f2bedde7 100644--- a/lib/composer_messages_finder.rb+++ b/lib/composer_messages_finder.rb@@ -206,7 +206,7 @@ class ComposerMessagesFindertopic_id: @details[:topic_id],)- reply_username = User.where(id: last_x_replies[0]).pluck_first(:username)+ reply_username = User.where(id: last_x_replies[0]).pick(:username){id: "get_a_room",diff --git a/lib/email/sender.rb b/lib/email/sender.rbindex 1f3c59efd04..bdbc312b9b8 100644--- a/lib/email/sender.rb+++ b/lib/email/sender.rb@@ -122,7 +122,7 @@ module Emailif from_address.blank?nilelse- Group.where(email_username: from_address, smtp_enabled: true).pluck_first(:id)+ Group.where(email_username: from_address, smtp_enabled: true).pick(:id)end)diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rbindex 03d181467bb..0536938c915 100644--- a/lib/file_store/s3_store.rb+++ b/lib/file_store/s3_store.rb@@ -409,7 +409,7 @@ module FileStoreverified_ids = []files.each do |f|- id = model.where("url LIKE '%#{f.key}' AND etag = '#{f.etag}'").pluck_first(:id)+ id = model.where("url LIKE '%#{f.key}' AND etag = '#{f.etag}'").pick(:id)verified_ids << id if id.present?marker = f.keyenddiff --git a/lib/freedom_patches/pluck_first.rb b/lib/freedom_patches/pluck_first.rbindex 6f3d520187f..616cbad93d5 100644--- a/lib/freedom_patches/pluck_first.rb+++ b/lib/freedom_patches/pluck_first.rb@@ -2,15 +2,17 @@class ActiveRecord::Relationdef pluck_first(*attributes)- limit(1).pluck(*attributes).first+ Discourse.deprecate("`#pluck_first` is deprecated, use `#pick` instead.")+ pick(*attributes)enddef pluck_first!(*attributes)- items = limit(1).pluck(*attributes)+ Discourse.deprecate("`#pluck_first!` is deprecated without replacement.")+ items = pick(*attributes)- raise_record_not_found_exception! if items.empty?+ raise_record_not_found_exception! if items.nil?- items.first+ itemsendenddiff --git a/lib/guardian.rb b/lib/guardian.rbindex 89ae923b18b..a34dadd7102 100644--- a/lib/guardian.rb+++ b/lib/guardian.rb@@ -368,8 +368,7 @@ class Guardiandef can_use_flair_group?(user, group_id = nil)return false if !user || !group_id || !user.group_ids.include?(group_id.to_i)- flair_icon, flair_upload_id =- Group.where(id: group_id.to_i).pluck_first(:flair_icon, :flair_upload_id)+ flair_icon, flair_upload_id = Group.where(id: group_id.to_i).pick(:flair_icon, :flair_upload_id)flair_icon.present? || flair_upload_id.present?enddiff --git a/lib/post_creator.rb b/lib/post_creator.rbindex b233de35403..2ce5a51307d 100644--- a/lib/post_creator.rb+++ b/lib/post_creator.rb@@ -328,9 +328,8 @@ class PostCreatorif PostCreator.track_post_statssequence = DraftSequence.current(@user, draft_key)revisions =- Draft.where(sequence: sequence, user_id: @user.id, draft_key: draft_key).pluck_first(- :revisions,- ) || 0+ Draft.where(sequence: sequence, user_id: @user.id, draft_key: draft_key).pick(:revisions) ||+ 0@post.build_post_stat(drafts_saved: revisions,diff --git a/lib/post_revisor.rb b/lib/post_revisor.rbindex 1ce1ef284be..e57fb42e542 100644--- a/lib/post_revisor.rb+++ b/lib/post_revisor.rb@@ -552,8 +552,7 @@ class PostRevisorif revision.modifications.empty?revision.destroy@post.last_editor_id =- PostRevision.where(post_id: @post.id).order(number: :desc).pluck_first(:user_id) ||- @post.user_id+ PostRevision.where(post_id: @post.id).order(number: :desc).pick(:user_id) || @post.user_id@post.version -= 1@post.public_version -= 1@post.save(validate: @validate_post)diff --git a/lib/search.rb b/lib/search.rbindex af244565081..8248975e978 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -430,7 +430,7 @@ class Searchadvanced_filter(/\Ain:wiki\z/i) { |posts, match| posts.where(wiki: true) }advanced_filter(/\Abadge:(.*)\z/i) do |posts, match|- badge_id = Badge.where("name ilike ? OR id = ?", match, match.to_i).pluck_first(:id)+ badge_id = Badge.where("name ilike ? OR id = ?", match, match.to_i).pick(:id)if badge_idposts.where("posts.user_id IN (SELECT ub.user_id FROM user_badges ub WHERE ub.badge_id = ?)",@@ -480,7 +480,7 @@ class Searchendadvanced_filter(/\Acreated:@(.*)\z/i) do |posts, match|- user_id = User.where(username: match.downcase).pluck_first(:id)+ user_id = User.where(username: match.downcase).pick(:id)posts.where(user_id: user_id, post_number: 1)end@@ -563,12 +563,12 @@ class Searchparent_category_id:Category.where("lower(slug) = ?", category_slug.downcase).select(:id),)- .pluck_first(:id)+ .pick(:id)elseCategory.where("lower(slug) = ?", category_slug.downcase).order("case when parent_category_id is null then 0 else 1 end")- .pluck_first(:id)+ .pick(:id)endif category_id@@ -579,7 +579,7 @@ class Searchposts.where("topics.category_id IN (?)", category_ids)else# try a possible tag match- tag_id = Tag.where_name(category_slug).pluck_first(:id)+ tag_id = Tag.where_name(category_slug).pick(:id)if (tag_id)posts.where(<<~SQL, tag_id)topics.id IN (@@ -625,7 +625,7 @@ class Searchgroup_query = cb.call(group_query, @term, @guardian)end- group_id = group_query.pluck_first(:id)+ group_id = group_query.pick(:id)if group_idposts.where(@@ -644,7 +644,7 @@ class Search.members_visible_groups(@guardian.user).where(has_messages: true).where("name ilike ? OR (id = ? AND id > 0)", match, match.to_i)- .pluck_first(:id)+ .pick(:id)if group_idposts.where(@@ -661,7 +661,7 @@ class SearchUser.where(staged: false).where("username_lower = ? OR id = ?", match.downcase, match.to_i)- .pluck_first(:id)+ .pick(:id)if user_idposts.where("posts.user_id = ?", user_id)else@@ -672,7 +672,7 @@ class Searchadvanced_filter(/\A\@(\S+)\z/i) do |posts, match|username = User.normalize_username(match)- user_id = User.not_staged.where(username_lower: username).pluck_first(:id)+ user_id = User.not_staged.where(username_lower: username).pick(:id)user_id = @guardian.user&.id if !user_id && username == "me"diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rakeindex 9fd1f480f3c..16f2e84f16a 100644--- a/lib/tasks/posts.rake+++ b/lib/tasks/posts.rake@@ -543,7 +543,7 @@ def recover_uploads_from_index(path).pluck(:post_id, :value).each do |post_id, uploads|uploads = JSON.parse(uploads)- raw = Post.where(id: post_id).pluck_first(:raw)+ raw = Post.where(id: post_id).pick(:raw)uploads.map! do |upload|orig = uploadif raw.scan(upload).length == 0diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex a7049b008e5..cbfbab113f2 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -630,8 +630,7 @@ class TopicQuerycategory_id = category_id_or_slug.to_iif category_id == 0- category_id =- Category.where(slug: category_id_or_slug, parent_category_id: nil).pluck_first(:id)+ category_id = Category.where(slug: category_id_or_slug, parent_category_id: nil).pick(:id)endcategory_id@@ -678,7 +677,7 @@ class TopicQueryfilter = (options[:filter] || options[:f])# category default sort ordersort_order, sort_ascending =- Category.where(id: category_id).pluck_first(:sort_order, :sort_ascending)+ Category.where(id: category_id).pick(:sort_order, :sort_ascending)if sort_order && (filter.blank? || %i[latest unseen].include?(filter))options[:order] = sort_orderoptions[:ascending] = !!sort_ascending ? "true" : "false"@@ -1023,7 +1022,7 @@ class TopicQuery:first_unread_pm_at,)else- UserStat.where(user_id: @user.id).pluck_first(:first_unread_pm_at)+ UserStat.where(user_id: @user.id).pick(:first_unread_pm_at)endquery = query.where("topics.updated_at >= ?", first_unread_pm_at) if first_unread_pm_atdiff --git a/lib/topic_query/private_message_lists.rb b/lib/topic_query/private_message_lists.rbindex 0c4874ba2a8..932a76c6de5 100644--- a/lib/topic_query/private_message_lists.rb+++ b/lib/topic_query/private_message_lists.rb@@ -188,7 +188,7 @@ class TopicQuerywhen :useruser_first_unread_pm_at(user)when :group- GroupUser.where(user: user, group: group).pluck_first(:first_unread_pm_at)+ GroupUser.where(user: user, group: group).pick(:first_unread_pm_at)elseuser_first_unread_pm_at = user_first_unread_pm_at(user)@@ -284,7 +284,7 @@ class TopicQueryenddef user_first_unread_pm_at(user)- UserStat.where(user: user).pluck_first(:first_unread_pm_at)+ UserStat.where(user: user).pick(:first_unread_pm_at)enddef group_with_messages_ids(user)diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex 576b6d3c5c3..b73e93a2588 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -671,7 +671,7 @@ class TopicViewenddef filtered_post_id(post_number)- @filtered_posts.where(post_number: post_number).pluck_first(:id)+ @filtered_posts.where(post_number: post_number).pick(:id)enddef is_mega_topic?@@ -679,7 +679,7 @@ class TopicViewenddef last_post_id- @filtered_posts.reverse_order.pluck_first(:id)+ @filtered_posts.reverse_order.pick(:id)enddef current_post_numberdiff --git a/lib/trust_level.rb b/lib/trust_level.rbindex 184b44ced6e..2589df081b9 100644--- a/lib/trust_level.rb+++ b/lib/trust_level.rb@@ -48,7 +48,7 @@ class TrustLevel.where(action: UserHistory.actions[:change_trust_level]).where(target_user_id: user.id).order(created_at: :desc)- .pluck_first(:new_value)+ .pick(:new_value).to_iendenddiff --git a/lib/validators/max_username_length_validator.rb b/lib/validators/max_username_length_validator.rbindex fe29824f834..49bcc1b400c 100644--- a/lib/validators/max_username_length_validator.rb+++ b/lib/validators/max_username_length_validator.rb@@ -13,7 +13,7 @@ class MaxUsernameLengthValidatorreturn falseendreturn false if value < SiteSetting.min_username_length- @username = User.where("length(username) > ?", value).pluck_first(:username)+ @username = User.where("length(username) > ?", value).pick(:username)@username.blank?enddiff --git a/lib/validators/min_username_length_validator.rb b/lib/validators/min_username_length_validator.rbindex 1ae9342fef1..ee0af94b698 100644--- a/lib/validators/min_username_length_validator.rb+++ b/lib/validators/min_username_length_validator.rb@@ -13,7 +13,7 @@ class MinUsernameLengthValidatorreturn falseendreturn false if value > SiteSetting.max_username_length- @username = User.where("length(username) < ?", value).pluck_first(:username)+ @username = User.where("length(username) < ?", value).pick(:username)@username.blank?enddiff --git a/script/import_scripts/base/lookup_container.rb b/script/import_scripts/base/lookup_container.rbindex 4147daf43f4..ab0fb3c6123 100644--- a/script/import_scripts/base/lookup_container.rb+++ b/script/import_scripts/base/lookup_container.rb@@ -58,7 +58,7 @@ module ImportScriptsdef 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 categorydiff --git a/script/micro_bench.rb b/script/micro_bench.rbindex 7ba5dceffe8..18aa85b019b 100644--- a/script/micro_bench.rb+++ b/script/micro_bench.rb@@ -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) }enddiff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rbindex c25cd5efbb9..50d6f419947 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -683,8 +683,7 @@ RSpec.describe ApplicationHelper doendit "returns two color scheme link tags when dark mode is enabled" do- SiteSetting.default_dark_mode_color_scheme_id =- ColorScheme.where(name: "Dark").pluck_first(:id)+ SiteSetting.default_dark_mode_color_scheme_id = ColorScheme.where(name: "Dark").pick(:id)cs_stylesheets = helper.discourse_color_scheme_stylesheetsexpect(cs_stylesheets).to include("(prefers-color-scheme: dark)")@@ -740,8 +739,7 @@ RSpec.describe ApplicationHelper dohelper.request.env[Auth::DefaultCurrentUserProvider::CURRENT_USER_KEY] = user@new_cs = Fabricate(:color_scheme, name: "Custom Color Scheme")- SiteSetting.default_dark_mode_color_scheme_id =- ColorScheme.where(name: "Dark").pluck_first(:id)+ SiteSetting.default_dark_mode_color_scheme_id = ColorScheme.where(name: "Dark").pick(:id)endit "returns no dark scheme stylesheet when user has disabled that option" dodiff --git a/spec/lib/wizard/step_updater_spec.rb b/spec/lib/wizard/step_updater_spec.rbindex 4c725b5051a..69648f8e2db 100644--- a/spec/lib/wizard/step_updater_spec.rb+++ b/spec/lib/wizard/step_updater_spec.rb@@ -370,7 +370,7 @@ RSpec.describe Wizard::StepUpdater doexpect(SiteSetting.contact_email).to eq("[email protected]")# Should update the TOS topic- raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)+ raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pick(:raw)expect(raw).to eq("ACME, Inc. - New Jersey law - Fairfield, New Jersey template")# Can update the TOS topic again@@ -382,13 +382,13 @@ RSpec.describe Wizard::StepUpdater docity_for_disputes: "San Francisco, California",)updater.update- raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)+ raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pick(:raw)expect(raw).to eq("Pied Piper Inc - California law - San Francisco, California template")# Can update the TOS to nothingupdater = wizard.create_updater("corporate", {})updater.update- raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)+ raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pick(:raw)expect(raw).to eq("company_name - governing_law - city_for_disputes template")expect(wizard.completed_steps?("corporate")).to eq(true)diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rbindex 74dcfe23f5f..25d160afb26 100644--- a/spec/models/post_action_spec.rb+++ b/spec/models/post_action_spec.rb@@ -49,13 +49,13 @@ RSpec.describe PostAction doexpect(topic_user_ids).to include(codinghorror.id)expect(topic_user_ids).to include(mod.id)- expect(topic.topic_users.where(user_id: mod.id).pluck_first(:notification_level)).to eq(+ expect(topic.topic_users.where(user_id: mod.id).pick(:notification_level)).to eq(TopicUser.notification_levels[:tracking],)- expect(- topic.topic_users.where(user_id: codinghorror.id).pluck_first(:notification_level),- ).to eq(TopicUser.notification_levels[:watching])+ expect(topic.topic_users.where(user_id: codinghorror.id).pick(:notification_level)).to eq(+ TopicUser.notification_levels[:watching],+ )# reply to PM should not clear flagPostCreator.new(diff --git a/spec/models/top_topic_spec.rb b/spec/models/top_topic_spec.rbindex f0fa30b4c94..0d057396fa5 100644--- a/spec/models/top_topic_spec.rb+++ b/spec/models/top_topic_spec.rb@@ -62,11 +62,11 @@ RSpec.describe TopTopic doTopTopic.refresh!top_topics = TopTopic.all- expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)- expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)+ expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(0.0000000001,).of(18.301029995664)- expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(0.0000000001,).of(10.602059991328)@@ -84,11 +84,11 @@ RSpec.describe TopTopic doTopTopic.refresh!top_topics = TopTopic.all- expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)- expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)+ expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(0.0000000001,).of(18.301029995664)- expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(0.0000000001,).of(11.2041199826559)@@ -106,11 +106,11 @@ RSpec.describe TopTopic doTopTopic.refresh!top_topics = TopTopic.all- expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(69)- expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(69)+ expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(0.0000000001,).of(33.301029995664)- expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(0.0000000001,).of(10.602059991328)@@ -128,11 +128,11 @@ RSpec.describe TopTopic doTopTopic.refresh!top_topics = TopTopic.all- expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(30)- expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(30)+ expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(0.0000000001,).of(21.301029995664)- expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(0.0000000001,).of(10.602059991328)@@ -144,11 +144,11 @@ RSpec.describe TopTopic doTopTopic.refresh!top_topics = TopTopic.all- expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)- expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)+ expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(0.0000000001,).of(18.301029995664)- expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(+ expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(0.0000000001,).of(10.602059991328)enddiff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rbindex cf837e44bc5..9e5177311f1 100644--- a/spec/models/topic_link_spec.rb+++ b/spec/models/topic_link_spec.rb@@ -53,8 +53,8 @@ RSpec.describe TopicLink doTopicLink.extract_from(post)# we have a special rule for images title where we pull them out of the filename- expect(topic.topic_links.where(url: png).pluck_first(:title)).to eq(png_title)- expect(topic.topic_links.where(url: non_png).pluck_first(:title)).to eq("amazing")+ expect(topic.topic_links.where(url: png).pick(:title)).to eq(png_title)+ expect(topic.topic_links.where(url: non_png).pick(:title)).to eq("amazing")expect(topic.topic_links.pluck(:url)).to contain_exactly(png,diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 793239bd8ab..fc4bf047952 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -64,7 +64,7 @@ RSpec.describe PostAlerter doPostAlerter.post_created(reply2)# we get a green notification for a reply- expect(Notification.where(user_id: pm.user_id).pluck_first(:notification_type)).to eq(+ expect(Notification.where(user_id: pm.user_id).pick(:notification_type)).to eq(Notification.types[:private_message],)@@ -104,7 +104,7 @@ RSpec.describe PostAlerter do)PostAlerter.post_created(op)- expect(Notification.where(user_id: user.id).pluck_first(:notification_type)).to eq(+ expect(Notification.where(user_id: user.id).pick(:notification_type)).to eq(Notification.types[:private_message],)enddiff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rbindex a38059c728f..9414a984288 100644--- a/spec/services/user_merger_spec.rb+++ b/spec/services/user_merger_spec.rb@@ -233,7 +233,7 @@ RSpec.describe UserMerger do[group1, group2, group3].each do |g|owner = [group1, group3].include?(g)expect(GroupUser.where(group_id: g.id, user_id: target_user.id, owner: owner).count).to eq(1)- expect(Group.where(id: g.id).pluck_first(:user_count)).to eq(2)+ expect(Group.where(id: g.id).pick(:user_count)).to eq(2)endexpect(GroupUser.where(user_id: source_user.id).count).to eq(0)end