繁体   English   中英

按计数的meta_search顺序

[英]meta_search order by count

我有这些模型,并且正在与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}

谢谢你帮我

这是我的情况,请在您的项目中尝试,我将竭尽所能:

所以我有car表,里面有carname_id和carmodel_id。 然后再添加2个包含名称和ID的汽车名称和车型的表格。 现在假设我的ID为2的梅赛德斯奔驰汽车,ID为5的S型轿车,在这种情况下,我的购物车将包含值2和5。

为了按照字段对它们进行排序,我这样做是:

在汽车控制器中:

  @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,“汽车制造”%>

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM