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 :shipping_profiles do |t|
t.string :dimensions_in, :default => {:width => 0, :height => 0, :depth => 0}
and just
t.string :dimensions_in, :default => Hash.new()
but the fields end up null. how can i set a default serialized object for this field on creation, or at least make sure my serialize attribute is always a hash?
When Rails serializes a hash to save in the db, all it does is convert it to YAML so that it can be stored as a string. To get this to work in the migration, all you need to do is convert the hash to yaml…
Or, alternatively, set it in the model after initialization…