簡體   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