algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

after_validations problematic with auto_indexing

Open vincentwoo opened this issue 8 years ago • 7 comments
trafficstars

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.

vincentwoo avatar Oct 03 '17 02:10 vincentwoo

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 ?

redox avatar Oct 03 '17 09:10 redox

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 avatar Oct 03 '17 10:10 vincentwoo

@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?

michniewicz avatar Oct 21 '17 21:10 michniewicz

@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 avatar Oct 22 '17 07:10 redox

@redox Curious why after_save hook isn't preferred in this case?

oyeanuj avatar Dec 02 '17 16:12 oyeanuj

@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.

redox avatar Dec 03 '17 17:12 redox

This issue really does need more attention, getting Algolia to behave correctly in Rails has remained very difficult.

vincentwoo avatar Feb 21 '19 23:02 vincentwoo