In Ruby/Sinatra with DataMapper and dm-types, in a model hook I have this block of code:
self.parent.meta[:post_count] += 1
self.parent.save
Unfortunately, that doesn’t work — the meta, which is a JSON type column, does not get updated. Help, please?
When you modify a complex property value, such as JSON, via its own API (#[] in your case) the dirty tracking system in DM is unfortunately bypassed which means your resource won’t be marked as dirty. It’s not a trivial issue to solve but sooner or later it will be done.
For now as a workaround you could override entire meta property value and increment the post_count, for instance:
self.parent.meta = parent.meta.merge("post_count" => parent.meta.fetch("post_count", 0)+1)I understand it doesn’t look nice but there’s no other way to do that now. You could encapsulate that code in a method like
increment_post_countto make it right.Also, please notice that you should use string keys rather than symbols.