如何通过ruby中的聚合函数来订购表?

我在Tender模型中有2个属性opening_dateclosing_date 。 在索引页面上,我显示的是剩余日期( closing_date-Date.today )。 但是,剩余日期不会保存在数据库中,但我想按剩余日期排序并显示表中的数据。 我该怎么用? 那有一个聚合函数吗?

我使用的是Ruby 1.9.2和Rails 3.2.2

// TendersController

  def index @tenders= Tender.where("company_id= ? ", current_user.company.id).order('closing_date') /* Here I am querying the tender by closing_date but I dont want to do that. What I wanted is I just want to order by (closing_date-Date.today) which is not in the database but should be done on the fly. ie the smallest the number it should be at the top of the table (index.html.erb) to notify the user the closing date is sooner than they think */ end 

//投标模型

 attr_accessible :title, :buyer_name, :category, :opening_date, :closing_date, :bid_amount, :description, :status 

//index.htm.erb

    <tr class=>   10%>    <% if day_diff =0 %> left  
Title Buyer Name Remaining dates Status Bid amount
<% if day_diff days passed

您可以使用sort_by ,但这是在ruby方面,因此在大型数据集上缓慢(数据库应该处理它)。 使用closing_date > NOW():order_by