default for serialized column in activerecord migration

前端 未结 3 490
自闭症患者
自闭症患者 2021-02-05 09:51

so i have a serialized column :dimensions, and in my migration, i would like to set the field to be a default hash.

i have tried...

create_table :shippin         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 10:19

    I tried this code and it worked for me. This takes advantage of the way ActiveRecord sends model methods synced to table columns dynamically.

    class MyModel < ActiveRecord::Base
    
      def dimensions_in
        attributes["dimensions_in"] ||= {:width => 0, :height => 0, :depth => 0}
      end
    
    end
    

提交回复
热议问题