ruby-on-rails - rails select查询为属性名称创建别名
问题描述
以创建 JSON 字符串为目标,要求是使用现有属性并在 JSON 输出中为其分配属性名称id
。
@valids = Destination.limit(10).select(:name, :long_name, :other_code).as_json(except: :id)
except: :id
正在调用以避免混淆,因为other_code
属性旨在成为生成的 JSON 中的 id。
然后将通过以下方式将其转换为有效的 JSON
@valids.to_json
如何other_code
输出为id
?
解决方案
您可以使用简单的字符串而不是符号来执行此操作。
Destination
.limit(10)
.select('name, long_name, other_code as id')
.as_json
推荐阅读
- ruby-on-rails - 如何使用 Nokogiri 从子字符串数组中抓取某些字符串的页面
- amazon-web-services - AWS 文档数据库中的事务
- angular - 如何加速 Angular 4 项目
- assembly - nasm 代码调用 scanf 崩溃取决于堆栈大小
- python - 使用pyinstaller打包selenium程序
- c# - C# ASP.NET MVC - 如果在 id 下缺少任何索引值,则动态添加文本框未绑定
- antd - TreeNode AntDesignPro 上的自定义工具提示
- git - 如何禁用每个 repo 的 GPG 签名
- excel - Oracle (ODBC) - excel 连接
- javascript - 谁能帮我如何使用ngrx本地存储来加密和解密数据