Search code examples
ruby-on-railsrubycollectpluck

What is simplest way to convert :pluck or :collect results to array of strings in Rails?


I have a model called Record and belongs Product model,

the price column type is hexadecimal. Rails already convert it to string in view. But I wanna get them in my console queries.

Example code for pluck:

Product.first.records.pluck(:price)

This query displays the values in array as hexadecimal. There is a method called to_sentences for pluck values but its not enough for my case.

The problem is same in collect method:

Product.first.records.collect(&:price)

What is the pluck query to display my hexadecimal data as array of strings like:

["45,46","75,42"]

Solution

  • Product.first.records.pluck(:price).map(&:to_s)