how to use jsonb in rails

僤鯓⒐⒋嵵緔 提交于 2019-12-01 00:30:59

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

Here is it how it worked with Rails 4.2

t.jsonb :slot_details, index: true, default: {}

The answer above threw errors for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!