I have a model and I would like to keep the identifiers of related objects (denormalize) for performance reasons. I have a method that looks like this:
def cache_ids
self._tag_ids = self.tag_ids
end
I thought I could just run it on before_save, however there is a problem - some of the related objects may be new entries and therefore they will not have identifiers.
Then I switched to after_save, but apparently this callback also starts before the transaction is completed, so the identifiers are not set yet.
At the moment, I am done with:
def save_with_idcache(*args)
return false unless save_without_idcache(*args)
cache_ids
return save_without_idcache(false)
end
alias_method_chain :save, :idcache
It seems to work, but it does not look very elegant.
Is there a better way? For example, does a callback persist after saving an object and related objects?
, - .