简体   繁体   English

在Rails中将:pluck或:collect结果转换为字符串数组的最简单方法是什么?

[英]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.

相关问题 在 Rails 中采摘和收集有什么区别? - What is the difference between pluck and collect in Rails? 在Rails中对字符串字段的一部分进行SQL查询的最简单方法是什么? - What is the simplest way to order results from a SQL query on part of a string field in Rails? 在Rails中存储〜1000个字符串元素数组的首选方法是什么? - What is the preferred way to store a ~1000 element array of Strings in Rails? 使一组方法可用于多个Rails应用程序的最简单方法是什么 - What is the simplest way to make a group of methods available to several Rails applications 在rails中使用小胡子视图模板的最简单方法是什么? - What's the simplest way to use mustache view templates in rails? 收集和存储物理地址或邮寄地址的规范 Rails 方式是什么? - What is the canonical Rails way to collect & store physical or mailing addresses? Rails 将字符串数组转换为 Flash 的无序列表 - Rails Convert Array of Strings to Unordered List for Flash 在elasticsearch中处理不是最简单的方法是什么? - what is simplest way to handle not in elasticsearch? 创建用于编辑模型属性(这是Rails中的字符串数组)的表单的最佳方法是什么? - What is the best way to create a form for editing a model attribute which is an array of strings in Rails? Rails可以收集所有路线 - Rails is there a way to collect all routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM