How to make Rails 4.2 work with Postgres Jsonb?
Asked Answered
S

1

7

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

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.

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

Sesquicentennial answered 13/12, 2014 at 19:46 Comment(0)
U
18

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

or add to an existing table

add_column :users, :extra_info, :jsonb

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.

Unflinching answered 13/12, 2014 at 20:45 Comment(1)
There are a number of operators made possible in Postgres 9.4. Checkout this article on some of those in the context of a Rails 4.2 app. robertbeene.com/rails-4-2-and-postgresql-9-4Doyledoyley

© 2022 - 2024 — McMap. All rights reserved.