Skip to content

Instantly share code, notes, and snippets.

@rbrooks
Created March 4, 2022 17:55
Show Gist options
  • Save rbrooks/f80011e2a60d0db03fc6b6e1b25ca770 to your computer and use it in GitHub Desktop.
Save rbrooks/f80011e2a60d0db03fc6b6e1b25ca770 to your computer and use it in GitHub Desktop.
# Sometimes you need to reindex all Rails Models. The reindex() method
# is specific to the SearchKick gem for ElasticSearch, and it only exists
# on ElasticSearch-indexed tables. We do a check for its existence below,
# or it would throw an error.
Dir[Rails.root.join('app/models/*.rb').to_s].each do |filename|
klass = File.basename(filename, '.rb').camelize.constantize
next unless klass.ancestors.include?(ActiveRecord::Base)
next if klass.abstract_class?
klass.reindex if klass.respond_to?(:reindex)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment