Search code examples
ruby-on-railsruby-on-rails-4pluck

How to use only use .pluck to grab the first x amount of records?


Can .pluck be used to get say only the first 5 values

e.g.

 CategoryItemKey.where(category_id: @category.id).pluck(:name)

I only want the first 5 names. Also if there are only 3 names it wont throw an error it will just grab 3.

Cant see it in the docs or from a google search. How would I accomplish this?


Solution

  • CategoryItemKey.where(category_id: @category.id).pluck(:name)[0...5]