Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goooooouwa/77c187bc2b726eeac170 to your computer and use it in GitHub Desktop.
Save goooooouwa/77c187bc2b726eeac170 to your computer and use it in GitHub Desktop.
work with ActiveRecord associations in Elasticsearch
if model A has one or many model B( equals model B belongs to model A), to update index of A when B gets updated, just write in B:
belongs_to A, touch: true
if model A belongs to model B( equals model B has one or many model A), to update index of A when B gets updated, just write in B:
has_many :As
after_update { self.As.each(&:touch) }
if model A has and belongs to many model B, to update index of A when one of Bs gets updated, just write in A
has_and_belongs_to_many :Bs, after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]
and in B
after_update { self.As.each(&:touch) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment