繁体   English   中英

如何在 ruby​​ on rails 中查询 Postgresql HSTORE

[英]How to query Postgresql HSTORE in ruby on rails

我需要找到类型面试用户的所有通知。

app/models/notification.rb

class Notification < ActiveRecord::Base
  belongs_to :user

  hstore_accessor(
    :data,
    path: :string,
    message: :string,
    type: :string
  )
end

在 google 中搜索没有太多信息,所以我遵循了 本教程:但无法按照示例中的说明查询数据库:

User.where("preferences -> newsletter = :value", value: 'true')

我试过

user.notifications.where('data -> type = :key', key: Interview::NOTIFICATION_TYPE)

但我得到了错误:

*** ActiveRecord::StatementInvalid Exception: PG::UndefinedColumn: ERROR:  column "type" does not exist
LINE 1: ...WHERE "notifications"."user_id" = $1 AND (data -> type = 'IN...
                                                             ^
: SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = $1 AND (data -> type = 'INTERVIEW')`

查询 HSTORE 的正确方法是注意键周围的单引号,如下所示:

user.notifications.where(
  "data -> 'type' = :key",
  key: Interview::NOTIFICATION_TYPE
).count

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM