如何在Rails中查询包含单引号的jsonb字段和值?

时间:2019-01-11 10:10:16

标签: ruby-on-rails postgresql-9.6

我有一个与PostgreSQL一起运行的Rails应用程序。有一个名为“程序”的表,如下所示:

Program(id: integer, name: string, properties: jsonb)

假设我们有一个名为“足球俱乐部”的程序,其属性如下:

{
  "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}'")

我知道问题是由我如何插入字符串引起的。但是,我找不到解决方案。 非常感谢您的帮助。

0 个答案:

没有答案