I have an observer that serializes a hash and saves it to a mySQL database as a TEXT field on my User model. The issue I’m having is I’m trying to add a new key/value to existing hash instead of overwriting it. It seems to simply be overwriting it.
Is my merge syntax incorrect?
Here’s my observer:
class NotificationObserver < ActiveRecord::Observer
observe :event
def after_update(event)
usersToNotify=event.following
usersToNotify.each do |u|
u.messages.merge! 'event'=>event.id
u.save
end
end
end
Any help is appreciated!
It works for me using Ruby 1.9.3 using both literals and implicit hashes. For example:
You might try using symbols instead of strings for your keys, unless you know you have a HashWithIndifferentAccess. You might also want to make sure you’re passing a hash literal by enclosing your hash in braces just to avoid any ambiguity with the parser.
There’s probably something else going on, so make sure you update your question with actual error messages and examples if the foregoing suggestions don’t work for you.