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 | |
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
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 18 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 10 |
2 files changed, 28 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 diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 82b15e565b..ec8bc913a1 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -793,6 +793,16 @@ class CalculationsTest < ActiveRecord::TestCase end end + def test_pick_one + assert_equal "The First Topic", Topic.order(:id).pick(:heading) + assert_nil Topic.where("1=0").pick(:heading) + end + + def test_pick_two + assert_equal ["David", "david@loudthinking.com"], Topic.order(:id).pick(:author_name, :author_email_address) + assert_nil Topic.where("1=0").pick(:author_name, :author_email_address) + end + def test_grouped_calculation_with_polymorphic_relation part = ShipPart.create!(name: "has trinket") part.trinkets.create! |