【问题标题】:postgresql and RoR - how quering a json type field where value is in an arraypostgresql 和 ForR - 如何查询值在数组中的 json 类型字段
【发布时间】:2014-11-09 00:18:39
【问题描述】:

假设我有一个带有 json 类型地址字段的 User 类,其中包含城市和国家/地区的键,如下所示:adress: {city: 'NY', country: 'USA'},我想获取所有居住在国家/地区名称数组中的用户,例如:country_names = ['Iran', 'Iraq', 'Yemen']。 我如何在 Ruby on Rails 中做到这一点?

我试过了:

User.where("address->>'country' IN ?", country_names)

但我得到一个语法错误。正确的方法是什么?

【问题讨论】:

    标签: ruby-on-rails ruby json postgresql


    【解决方案1】:

    IN 子句的值列表应该用括号括起来:

    User.where("address->>'country' IN (?)", country_names)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      相关资源
      最近更新 更多