ruby on rails - default for serialized column in activerecord migration -
so have serialized column :dimensions, , in migration, set field default hash.
i have tried...
create_table :shipping_profiles |t| t.string :dimensions_in, :default => {:width => 0, :height => 0, :depth => 0}
and just
t.string :dimensions_in, :default => hash.new()
but fields end null. how can set default serialized object field on creation, or @ least make sure serialize attribute hash?
when rails serializes hash save in db, convert yaml can stored string. work in migration, need convert hash yaml...
t.string :dimensions_in, :default => {:width => 0, :height => 0, :depth => 0}.to_yaml
or, alternatively, set in model after initialization...
class shippingprofile < activerecord::base after_initialize :set_default_dimensions private def set_default_dimensions self.dimensions_in ||= {:width => 0, :height => 0, :depth => 0} end end
Comments
Post a Comment