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