Using the new ActiveRecord::Store for serialization, the docs give the following example implementation:
class User < ActiveRecord::Base
store :settings, accessors: [ :color, :homepage ]
end
Is it possible to declare attributes with default values, something akin to:
store :settings, accessors: { color: 'blue', homepage: 'rubyonrails.org' }
?
No, there’s no way to supply defaults inside the
storecall. Thestoremacro is quite simple:And all
store_accessordoes is iterate through the:accessorsand create accessor and mutator methods for each one. If you try to use a Hash with:accessorsyou’ll end up adding some things to yourstorethat you didn’t mean to.If you want to supply defaults then you could use an
after_initializehook: