【问题标题】:meta_search order by countmeta_search 按计数排序
【发布时间】:2011-10-10 08:26:07
【问题描述】:

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

谢谢你帮助我。

【问题讨论】:

标签: ruby-on-rails meta-search


【解决方案1】:

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

所以我有带有 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"%>

现在一切都很好。

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2016-01-08
    • 2023-03-06
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多