Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use jsonb in rails

I have a rails project with Postgresql 9.4 as backend. I have column like this:

t.json :slot_details, null: false, default: {}

How do I change this to JSONB from JSON? Should I add index and that will be changed to JSONB?

like image 983
Aravind Avatar asked Sep 16 '25 11:09

Aravind


1 Answers

For migrating this, you can do the following. Payload in this case was originally a json field.

  class AlterJsonbToJsonAndBack < ActiveRecord::Migration                      
    def up                                                                     
      change_column :dynamics, :payload, 'jsonb USING CAST(payload AS jsonb)'  
    end                                                                        

    def down                                                                   
      change_column :dynamics, :payload, 'json USING CAST(payload AS json)'    
    end                                                                        
  end                                                                          

To find out how to query jsonb in Rails 4.2, checkout this article

like image 180
Robert Avatar answered Sep 19 '25 06:09

Robert