Skip to content
This repository
Browse code

Restore support for Model.pluck('sql fragment')

  • Loading branch information...
commit 3f352d040575c9a08d13e10fd6191fbf2fa674ba 1 parent 9b4f504
Jeremy Kemper authored May 15, 2012
3  activerecord/lib/active_record/relation/calculations.rb
@@ -130,7 +130,8 @@ def pluck(column_name)
130 130
       column = types[key]
131 131
 
132 132
       result.map do |attributes|
133  
-        value = klass.initialize_attributes(attributes)[key]
  133
+        raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one?
  134
+        value = klass.initialize_attributes(attributes).first[1]
134 135
         if column
135 136
           column.type_cast value
136 137
         else
8  activerecord/test/cases/calculations_test.rb
@@ -466,6 +466,14 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
466 466
     assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
467 467
   end
468 468
 
  469
+  def test_pluck_with_selection_clause
  470
+    assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT credit_limit').sort
  471
+  end
  472
+
  473
+  def test_pluck_expects_a_single_selection
  474
+    assert_raise(ArgumentError) { Account.pluck 'id, credit_limit' }
  475
+  end
  476
+
469 477
   def test_plucks_with_ids
470 478
     assert_equal Company.all.map(&:id).sort, Company.ids.sort
471 479
   end

0 notes on commit 3f352d0

Please sign in to comment.
Something went wrong with that request. Please try again.