Skip to content

Instantly share code, notes, and snippets.

@lalitlogical
Created December 18, 2018 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalitlogical/2420c4c14892d82557ba8559a7d2d986 to your computer and use it in GitHub Desktop.
Save lalitlogical/2420c4c14892d82557ba8559a7d2d986 to your computer and use it in GitHub Desktop.
searchkick mongoid
Searchkick.class_eval do
# Your new methods here
def self.load_records(records, ids)
records =
if records.respond_to?(:queryable)
# Mongoid 3+
records.queryable.for_ids(ids)
elsif records.respond_to?(:primary_key)
# ActiveRecord
records.where(records.primary_key => ids) if records.primary_key
elsif records.respond_to?(:unscoped) && :id.respond_to?(:in)
# Nobrainer
records.unscoped.where(:id.in => ids)
elsif records.respond_to?(:key_column_names)
records.where(records.key_column_names.first => ids)
end
raise Searchkick::Error, "Not sure how to load records" if !records
records
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment