开发者

Adding additional sorting attributes to array of records...then sorting!

开发者 https://www.devze.com 2023-01-04 05:50 出处:网络
Let\'s say I run an ActiveRecord query and it returns 3开发者_开发问答 results, with a bunch of attributes.

Let's say I run an ActiveRecord query and it returns 3开发者_开发问答 results, with a bunch of attributes.

I want to add 3 attributes to each record in the Array so I can do sorting at a code-level. These 3 include: num_comments, num_views, last_view.

  1. How do I add these attributes?
  2. How do I then sort the records, in order of precendence, by num_comments AND by num_views AND by last_view?


# file a_model_class (AR subclass)
   attr_accessor :num_comments, :num_views, :last_view # add setters and getters

# different file that is doing the fetch and sort...
results = AModelClass.find.... # 3 results
results.each{|r|
  r.num_comments = x  # set the instance variables for sorting
  r.num_views = y
  r.last_view = z
}

# now sort
sorted_results = results.sort{|a,b|
  r = a.num_comments <=> b.num_comments 
  r = a.num_views <=> b.num_views if r == 0
  r = a.last_view <=> b.last_view if r == 0
}

# sorted_results is sorted copy of results


Expanding on Larry K's answer and zed_0xff's comment, why even create attributes, just sort by a method in your model:

class Widget < ActiveRecord::Base
  def num_comments
    #count comments
  end

  def num_views
    #count views
  end

  def last_view
    #return last view
  end
end

in your controller

class WidgetsController < ApplicationController
  def index
    @widgets = Widget.all.sort_by{ |r| [r.num_comments, r.num_views, r.last_view] }
  end
end
0

精彩评论

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