Method: Dynamoid::Criteria::Chain#pluck
- Defined in:
- lib/dynamoid/criteria/chain.rb
#pluck(*args) ⇒ Array
Select only specified fields.
It takes one or more field names and returns an array of either values or arrays of values.
Post.pluck(:id) # => ['1', '2']
Post.pluck(:title, :title) # => [['1', 'Title #1'], ['2', 'Title#2']]
Post.where('views_count.gt' => 1000).pluck(:title)
There are some differences between pluck and project. pluck
-
doesn’t instantiate models
-
it isn’t chainable and returns
Arrayinstead ofChain
It deserializes values if a field type isn’t supported by DynamoDB natively.
It can be used to avoid loading large field values and to decrease a memory footprint.
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/dynamoid/criteria/chain.rb', line 490 def pluck(*args) fields = args.map(&:to_sym) # `project` has a side effect - it sets `@project` instance variable. # So use a duplicate to not pollute original chain. scope = dup scope.project(*fields) if fields.many? scope.items.map do |item| fields.map { |key| Undumping.undump_field(item[key], source.attributes[key]) } end.to_a else key = fields.first scope.items.map { |item| Undumping.undump_field(item[key], source.attributes[key]) }.to_a end end |