Skip to content

Instantly share code, notes, and snippets.

@smtlaissezfaire
Created November 21, 2014 07:10
Show Gist options
  • Save smtlaissezfaire/4aa60d808d9da7758790 to your computer and use it in GitHub Desktop.
Save smtlaissezfaire/4aa60d808d9da7758790 to your computer and use it in GitHub Desktop.
in_array
def first(options={})
return nil if ids.blank?
if sorted?
matched_ids = klass.collection.distinct(:_id, query(options).criteria.to_hash)
return nil if matched_ids.blank?
matched_ids.sort_by! { |matched_id| ids.index(matched_id) }
find!(matched_ids.first)
else
query(options).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment