Rails 3 - How to Create a JSON Object to store in the database

后端 未结 3 552
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 07:33

I\'m creating an AuditLog observer that watches over several models.

I\'d like the observer to have an after_create, which creates a JSON object that is stored in a data

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 07:46

    Current versions of rails support json serialisation out of the box. Define your field as a string (or text) in the migration and then:

    class Foo < ActiveRecord::Base
      serialize :field, JSON
    end
    
    bar = Foo.new
    bar.field = [1,2,3]
    bar.save
    

提交回复
热议问题