Created
November 21, 2014 07:10
-
-
Save smtlaissezfaire/4aa60d808d9da7758790 to your computer and use it in GitHub Desktop.
in_array
This file contains 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
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