diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-02-09 10:30:19 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-09 10:30:19 -0800 |
commit | 80cc0d323ba08a51d4299cb4fc34f157818fead4 (patch) | |
tree | 78b41781f1d67b810dc781084a5971ea469424bf /activerecord/lib/active_record | |
parent | b6ee4e4b989bfc2fe50b2b830fdcb90ef9e23d9a (diff) | |
download | rails-80cc0d323ba08a51d4299cb4fc34f157818fead4.tar.gz rails-80cc0d323ba08a51d4299cb4fc34f157818fead4.tar.bz2 rails-80cc0d323ba08a51d4299cb4fc34f157818fead4.zip |
Add Relation#pick as short-hand for single-value plucks (#31941)
* Add Relation#pick as short-hand for single-value plucks
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index dba2f33dd4..6a3bd6a1f9 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -200,6 +200,24 @@ module ActiveRecord end end + # 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' ] + def pick(*column_names) + limit(1).pluck(*column_names).first + end + # Pluck all the ID's for the relation using the table's primary key # # Person.ids # SELECT people.id FROM people |