Last active
March 13, 2019 08:29
-
-
Save goooooouwa/77c187bc2b726eeac170 to your computer and use it in GitHub Desktop.
work with ActiveRecord associations in Elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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