how to use jsonb in rails

前端 未结 2 582
温柔的废话
温柔的废话 2021-01-06 18:24

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 ch

相关标签:
2条回答
  • 2021-01-06 18:43

    Here is it how it worked with Rails 4.2

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

    The answer above threw errors for me.

    0 讨论(0)
  • 2021-01-06 18:58

    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

    0 讨论(0)
提交回复
热议问题