0

我有这些模型,我正在使用 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}

谢谢你帮助我。

4

1 回答 1

0

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

所以我有 carname_id 和 carmodel_id 的汽车表。然后还有 2 个用于汽车名称和汽车型号的表,其中包含名称和 ID。现在假设我有 id 为 2 的 carname Mercedes 和 id 为 5 的 carmodel S classe,在这种情况下,我的 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"%>

现在一切都好。

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

<%= sort_link @search, :company_estimate, "汽车制造" %>

希望能帮助到你。

于 2011-10-15T01:05:00.797 回答