Cu1ture
Cu1ture

Reputation: 1283

Set "acts-as-taggable-on" Taggings association to touch Taggable on save?

I am currently using the acts-as-taggable-on gem to add tags to my Pieces model.

I am using elasticsearch to add an index of the tags to Pieces.

However to update the index when a tagging is created I need to make the associations

belongs_to :taggable, touch: true

In the Tagging model.

Here is a link to the Tagging model in acts-as-taggable-on

How can I add the touch: true association to the Taggings model so that my Piece index is updated when I create a Tagging for that piece?

Upvotes: 4

Views: 663

Answers (1)

Cu1ture
Cu1ture

Reputation: 1283

Ok I figured it out.

There are two steps to this. First you must set up the belongs_to association in the Tagging model to include the "touch: true" option. This can be done in the acts_as_taggable.rb initializer like so:

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

Next we need to tell elasticsearch to reindex the Piece index every time a Piece is touched. This can be done by adding the following callback to the Piece model:

after_touch() { __elasticsearch__.index_document }

Upvotes: 3

Related Questions