On Friday, July 3, 2015 at 7:33:50 AM UTC-7, [email protected] wrote: > > Hello, > > I have a model with a JSONB column. I'd like to be able to access this > column as both a hash and a class (let's say an openstruct for the example). > > I can do it the obvious way (with a location JSONB column) > > > > def location_struct=(loc) > self.location = loc.to_h > end > > def location_struct > location && OpenStruct.new(location).freeze > end > > > But it's not very friendly. > > Do you have any best practice for this kind of scenario or is the above > approach not that bad (in a plugin)? >
You might want to consider using the composition plugin. The main issue with the code you posted is you are creating a new frozen OpenStruct on every call to location_struct. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
