Created
October 23, 2010 09:14
-
-
Save maricris-sn/641980 to your computer and use it in GitHub Desktop.
As seen in: http://coderkitty.sweetperceptions.com/2009/3/27/removing-out-of-sync-error-in-acts_as_solr
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 reorder(things, ids) | |
ordered_things = Array.new(things.size) | |
unless things.size == ids.size | |
(ids - (things.collect{|x| x.id})).collect{|missing| ids[ids.index(missing)] = nil} | |
ids = ids.compact | |
end | |
raise "Out of sync! Found #{ids.size} items in index, but only #{things.size} were found in database! Remove #{(ids - (things.collect{|x| x.id})).to_sentence}." unless things.size == ids.size | |
things.each do |thing| | |
position = ids.index(thing.id) | |
ordered_things[position] = thing | |
end | |
ordered_things | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment