Sorting fetched collection from ActiveRecord or ThinkingSphinx
I have two questions.
Is there a way to sort collection from ActiveRecord or Thinking Sphinx without re-select everything from database/sphinx? ie.
@models = Model.where("foo = :foo", {:foo => params[:foo]})
models.some_code_or_method_which_will_resort_everything
Is this game worth a candle(sorting array/collection without fetching again)? I wonder which option is better for performance.
Thanks in advance.
Edit
So, this game is worth a candle when you:
- don't use pagination(you must have all records)
- refresh page div with ajax(don't fetch everything again with calling index or show action)
Art for art's sake开发者_如何学Python..
I'm pretty much sure 'sort' should work:
@models.sort! { |a,b| a.foo <=> b.foo }
Answering the second question, yes, it does. It pretty much depends on the amount of objects in your collection, but generally it is usually a performance win
精彩评论