如何使 Rails 4.2 与 Postgres Jsonb 一起工作? [英] How to make Rails 4.2 work with Postgres Jsonb?

查看:33
本文介绍了如何使 Rails 4.2 与 Postgres Jsonb 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些博客 帖子 声称 rails 4.2 在 Postgres 4.2 中添加了对新 Jsonb 数据类型的支持.

I've seen a few blog posts claiming that rails 4.2 added support for the new Jsonb data type in Postgres 4.2.

然而,谷歌搜索让我在如何实际使用数据类型方面得到零结果.由于我不依赖于键顺序并且我希望我的应用程序快速,我非常希望在我的模型之一中使用 Jsonb 而不是 json.

However, googling gets me zero results on how to actually use the datatype. Since I'm not depending on key order and I would like my application to be fast, I would very much like to use Jsonb instead of json in one of my models.

它实际上是在 4.2 中添加的吗?如果是,您将如何使用它?

Was it actually added in 4.2, and, if so, how do you use it?

推荐答案

它是尚未发布的 Rails 4.2 版本(当前为 4.2.0.rc3)的一部分.要使用数据类型,请在创建表时指定 jsonb 类型:

It's part of the not yet released version of Rails 4.2 (currently 4.2.0.rc3). To use the datatype, specify the jsonb type when creating a table:

create_table :users do |t|
  t.jsonb :extra_info
end

或添加到现有表格

add_column :users, :extra_info, :jsonb

由于 jsonbjson 几乎 相同,除了内部存储,您处理列的方式 也是一样的.

Since jsonb is virtually the same as json except for the internal storage, the way you work with the column is the same as well.

这篇关于如何使 Rails 4.2 与 Postgres Jsonb 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆