Link "act-as-taggable-on" Taggings to touch Taggable on save?

I am currently using the act-as-taggable-on shortcut to add tags to the Pieces model.

I am using elasticsearch to add a tag index to Pieces.

However, to update the index when creating tags, I need to create associations

belongs_to :taggable, touch: true

      

In the Tagging model.

Here is a link to the Tagging model in actions like-taggable-on

How can I add anchor: true linkage to the Taggings model to update the index of the Piece when I create tagging for that piece?

+3


source to share


1 answer


Ok I figured it out.

There are two steps to this. First, you must configure the belongs_to association in the Tagging model to enable the "touch: true" option. This can be done in the act_as_taggable.rb initializer as follows:

ActsAsTaggableOn::Tagging.belongs_to :taggable, polymorphic: true, touch: true

      



Next, we need to tell elasticsearch to re-index the Piece's index every time the Piece touches. This can be done by adding the following callback to the Piece model:

after_touch() { __elasticsearch__.index_document }

      

+1


source







All Articles