DEV: pluck_first

Doing .pluck(:column).first is a very common pattern in Discourse and in
most cases, a limit cause isn't being added. Instead of adding a limit
clause to all these callsites, this commit adds two new methods to
ActiveRecord::Relation:

pluck_first, equivalent to limit(1).pluck(*columns).first

and pluck_first! which, like other finder methods, raises an exception
when no record is found
This commit is contained in:
Daniel Waterworth
2019-10-21 11:32:27 +01:00
parent 72822aa93f
commit 55a1394342
43 changed files with 99 additions and 80 deletions

View File

@@ -7,7 +7,7 @@ class MaxUsernameLengthValidator
def valid_value?(value)
return false if value < SiteSetting.min_username_length
@username = User.where('length(username) > ?', value).pluck(:username).first
@username = User.where('length(username) > ?', value).pluck_first(:username)
@username.blank?
end

View File

@@ -7,7 +7,7 @@ class MinUsernameLengthValidator
def valid_value?(value)
return false if value > SiteSetting.max_username_length
@username = User.where('length(username) < ?', value).pluck(:username).first
@username = User.where('length(username) < ?', value).pluck_first(:username)
@username.blank?
end