[英]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, 我有一个称为Record的模型,并且属于Product模型,
the price
column type is hexadecimal. price
列类型为十六进制。 Rails already convert it to string in view. Rails已经在视图中将其转换为字符串。 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. 有一个称为
to_sentences
的方法to_sentences
用于获取值,但对于我的情况来说还不够。
The problem is same in collect
method: 问题在
collect
方法中是一样的:
Product.first.records.collect(&:price)
What is the pluck query to display my hexadecimal data as array of strings like: 什么是将我的十六进制数据显示为以下字符串数组的查询:
["45,46","75,42"]
Product.first.records.pluck(:price).map(&:to_s)
请尝试以下操作:
Product.first.records.pluck(:price).join(',')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.