簡體   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