ruby on rails - Convert serialized-hash to Model attributes -
i have serialized data on activerecord model, , want delegate attributes it. this:
class object < ar::base serialize :data delegate :title, to: :data end
but of course doesn't work hash. there other way?
what want in end this. give activerecord model list of symbols:
[ :title, :size, :color ]
they transformed getters , setters so:
def title data[:title] end def title=(val) data[:title]= val end
and represented next rest of ar attributes:
#<model id: 21, title: "foo", size: 4, color: nil>
you looking store
:
class user < activerecord::base store :settings, accessors: [ :color, :homepage ] end u = user.new(color: 'black', homepage: '37signals.com') u.color # accessor stored attribute u.settings[:country] = 'denmark' # attribute, if not specified accessor
Comments
Post a Comment