instance method
pick
Ruby on Rails 7.2.3
Since v6.0.6Signature
pick(*column_names)
Pick the value(s) from the named column(s) in the current relation. This is short-hand for relation.limit(1).pluck(*column_names).first, and is primarily useful when you have a relation that’s already narrowed down to a single row.
Just like #pluck, #pick will only load the actual value, not the entire record object, so it’s also more efficient. The value is, again like with pluck, typecast by the column type.
Person.where(id: 1).pick(:name) # SELECT people.name FROM people WHERE id = 1 LIMIT 1 # => 'David' Person.where(id: 1).pick(:name, :email_address) # SELECT people.name, people.email_address FROM people WHERE id = 1 LIMIT 1 # => [ 'David', 'david@loudthinking.com' ]
Parameters
-
column_namesrest
Source
# File activerecord/lib/active_record/relation/calculations.rb, line 347
def pick(*column_names)
if loaded? && all_attributes?(column_names)
result = records.pick(*column_names)
return @async ? Promise::Complete.new(result) : result
end
limit(1).pluck(*column_names).then(&:first)
end
Defined in activerecord/lib/active_record/relation/calculations.rb line 347
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Calculations