algoliasearch-rails
algoliasearch-rails copied to clipboard
after_validations problematic with auto_indexing
Right now, you use after_validation callbacks to trigger indexing:
after_validation :algolia_mark_must_reindex if respond_to?(:after_validation)
This is sort of troublesome, because a user cannot attach a before_save hook that fires before Algolia's, because after_validation will always fire first. If a user wants to make a change in a before_save hook that would trigger an Algolia update, it will be ignored, because Algolia will have looked at the model before the change has happened.
Again, I think the user needs more control over when a model gets flagged for indexing.
This is sort of troublesome, because a user cannot attach a before_save hook that fires before Algolia's, because after_validation will always fire first. If a user wants to make a change in a before_save hook that would trigger an Algolia update, it will be ignored, because Algolia will have looked at the model before the change has happened.
That's a good point @vincentwoo. I try to avoid as much as much updates inside before_save callbacks, but I assume you cannot do that somewhere else? Why going for before_save instead of before_validation if it's to update the object?
Again, I think the user needs more control over when a model gets flagged for indexing.
If we introduce such a method, responsible of flagging or not an object for indexing; when do you think we should call it? after_commit ?
I think in Rails land it's a bit more typical to do this sort of thing in before_save, as generally you will not be modifying data in such a way as to require validation. That said, you have a point.
I think you should ask the model as late as possible in the save lifecycle about whether it should be persisted to Algolia. Communicating to Algolia is a totally independent, async operation that doesn't really need to be tied into the ActiveRecord lifecycle. However, the one caveat is that having access to the *_changed? helpers is very useful and thus precludes after_commit.
@vincentwoo, @redox additionally to all above, after_validation hook is triggered every time even if the validation is failed. Does it make sense to call algolia_mark_must_reindex in that case?
@vincentwoo, @redox additionally to all above, after_validation hook is triggered every time even if the validation is failed. Does it make sense to call algolia_mark_must_reindex in that case?
It would mean we should then find a way to assess whether previous validations failed to not run the indexing.
@redox Curious why after_save hook isn't preferred in this case?
@redox Curious why after_save hook isn't preferred in this case?
The idea was to not rely on any *_save callback because they are still called while/if the underlying transaction fails.
This issue really does need more attention, getting Algolia to behave correctly in Rails has remained very difficult.