meta_search按计数排序

时间:2011-10-10 08:26:08

标签: ruby-on-rails meta-search

我有这些模型,我正在玩meta_search来订购它。

class Company < ActiveRecord::Base
  has_many :delivers
  has_many :estimates, :through => :deliver
end

class Estimate < ActiveRecord::Base
  has_many :delivers, :dependent => :destroy
  has_many :companies, :through => :delivers
end

class Deliver < ActiveRecord::Base
  belongs_to :estimate
  belongs_to :company
end

正如您所看到的,公司有很多估算,我需要一个过滤器来按此顺序显示公司:从估算数量最多的公司到估算数量最少的公司。
我希望有一个排序链接,例如:

= sort_link @search, :estimates_asc

我知道我可以通过这种方式订购公司

Company.joins(:estimates).sort_by{|x| -x.estimates.size}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是我的情况并在您的项目中尝试,我将尽我所能帮助您:

所以我有carname_id和carmodel_id的汽车表。然后还有2个表,其中包含名字和身份证的卡通名人和名模。现在让我们说我的车牌名为Mercedes,身高为2,carmodel S classe为id 5,在这种情况下,我的cartable将包含值2和5 Mercedes S classe。

按字段对字段进行排序我是这样做的:

在汽车控制器:

  @search = Car.search(params[:search])
    @search.meta_sort ||= 'created_at.desc'  <-- this line is for default order display, you can use any other field
    @cars = @search.all.paginate :page => params[:page], :per_page => 5

并在视图

<%= sort_link @search, :carname_name,  "Car make" %>  <-- carname is the field from 

    car table where the carname id is stored and name is the he name itself for this id that's in carname table

<%= sort_link @search, :carmodel_name, "Car model"%>

现在一切都很好。

在你的情况下,我只是假设它会像

&lt;%= sort_link @search,:company_estimate,“Car make”%&gt;

希望它有所帮助。