首页 > 解决方案 > jsonb 值的条件

问题描述

在我的翻译表中,我有jsonb: values where we can find per example: {"en" =>"City"} 我正在尝试获取所有未翻译的翻译,我尝试过:

Translation.where(values.values="")

和类似的东西

Translation.all.values.each do |language, translation|
  if translation.empty?
    #here I don't know how to should I do to get all the un-translated 
    #translations without overwriting
end

我怎样才能正确地做到这一点?

标签: ruby-on-railsjsonjsonb

解决方案


对于空的 json 表字段,例如 Translation.values = {}

Translation.where(values = '{}'")

对于 json 表字段中的空键,例如 Translation.values = { "mykey": "" }

Translation..where("(values-> 'mykey') IS NULL")

PostgreSQL json 运算符:https ://www.postgresql.org/docs/9.5/static/functions-json.html


推荐阅读