Commit 21eccec4df0a508c8610960c502ce815e61d7fa4
1 parent
7c6e8d4c
Exists in
relation
Pluck empty [skip ci]
Showing
2 changed files
with
15 additions
and
1 deletions
Show diff stats
lib/searchkick/relation.rb
... | ... | @@ -116,11 +116,18 @@ module Searchkick |
116 | 116 | |
117 | 117 | # TODO make more efficient if loaded |
118 | 118 | def pluck(*fields) |
119 | - result = select(*fields).load(false) | |
119 | + result = load(false) | |
120 | + if fields.empty? | |
121 | + result.select!(true) | |
122 | + else | |
123 | + result.select!(*fields) | |
124 | + end | |
125 | + | |
120 | 126 | if fields.size == 1 |
121 | 127 | field = fields.first |
122 | 128 | result.map { |v| v[field] } |
123 | 129 | else |
130 | + fields = result.first.keys if fields.empty? && result.any? | |
124 | 131 | result.map { |v| fields.map { |f| v[f] } } |
125 | 132 | end |
126 | 133 | end | ... | ... |
test/relation_test.rb
... | ... | @@ -45,6 +45,13 @@ class RelationTest < Minitest::Test |
45 | 45 | assert_equal [["Blue", nil], ["Red", nil]], Product.search.order(:name).pluck(:name, :store_id) |
46 | 46 | end |
47 | 47 | |
48 | + def test_pluck_empty | |
49 | + Product.search_index.refresh | |
50 | + assert_equal [], Product.search.pluck | |
51 | + store_names ["Blue", "Red"] | |
52 | + assert Product.search.pluck | |
53 | + end | |
54 | + | |
48 | 55 | def test_parameters |
49 | 56 | skip unless defined?(ActiveRecord) |
50 | 57 | require "action_controller" | ... | ... |