I'm using an ActiveRecord::Observer to log users changing data in my
application. However, I've noted that adding an item to a has_many
relationship gets logged, but removing the item does not. That is:
@product.users << user
is logged, but:
@product.users.delete(user)
is not.
I've dug into ActiveRecord and found that the underlying issue is that
the HasManyAssociation delete_records method uses update_all to update
the record. update_all bypasses the normal call backs and therefore also
bypasses the Observer.
Is there a way to get an Observer to observe update_all calls?
And call one of @activity_logger's public methods to generate the log.
def log_removal(item)
get_an_active_activity_logger
@activity_logger.log_activity(
self,
'item_removal',
"#{item.class.name} #{item.id} removed from association"
)
end
It means I have to update each of my has_many calls and make the
log_removal and get_an_active_activity_logger methods available to the
models where they are needed, but both of those are fairly trivial
tasks.