开发者

Sorting fetched collection from ActiveRecord or ThinkingSphinx

I have two questions.

  1. 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

  2. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜