ruby-on-rails - 如何在 Rails 中查询包含单引号的 jsonb 字段和值?
问题描述
我有一个使用 PostgreSQL 运行的 Rails 应用程序。有一个名为 Programs 的表,如下所示:
Program(id: integer, name: string, properties: jsonb)
假设我们有一个名为“Football Club”的程序,其属性如下:
{
"properties" =>
{
"What's your gender?" => "I'm male.",
"Is your health good?" => "Yes"
}
}
我的查询是这样的:
# this field and value are OK with the query
field = Is your health good?
value = Yes
Program.where("properties ->> '#{field}') = '#{value}'")
# this field and value causes error with the query since they contain quote (')
field = What's your gender?
value = I'm male.
Program.where("properties ->> '#{field}') = '#{value}'")
我知道问题是由我插入字符串的方式引起的。然而,我找不到解决方案。非常感谢您的帮助。
解决方案
推荐阅读
- ios - 一旦 UICollectionViewCell 变得可见,如何在它上调用 UI 函数
- php - HTML使链接可点击
用 PHP 中的文本填充的元素
- postgresql - Postgresql 自动增量在 Ubuntu 中不起作用
- slack - 如何通过 Slack OpsGenie 集成通知团队成员?
- javascript - 根据文本长度和容器大小调整字体大小
- github - 如果 Git 中的多个远程存储库包含一个名称相同但数据不同的分支,我该如何处理它们?
- excel - ExportAsFixedFormat 为 PDF System.Runtime.InteropServices.COMException:
- r - 函数取决于 R 中的行中的值
- sql - 删除 SQL Server 查询中的前导 0
- python - Python re.match - 一个数字的成功匹配结果是一个字母?