[英]meta_search order by count
I have these models and I'm playing with meta_search to order it. 我有这些模型,并且正在与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
As you can see a company has many estimates and I need a filter to show companies in this order: from company who has the highest number of estimates to company that has the lowest number of estimates. 如您所见,一家公司有很多估算值,我需要一个过滤器来按此顺序显示公司:从估算值最高的公司到估算值最低的公司。
I'd like to have a sort link for it, something like: 我想要一个排序链接,例如:
= sort_link @search, :estimates_asc
I know that I can order in that way companies with this statement 我知道我可以以此方式订购公司
Company.joins(:estimates).sort_by{|x| -x.estimates.size}
Thanks for helping me. 谢谢你帮我
This is my case and try it in your project, I'll help you with what I can: 这是我的情况,请在您的项目中尝试,我将竭尽所能:
So I have cars table with carname_id and carmodel_id in it. 所以我有car表,里面有carname_id和carmodel_id。 then 2 more tables for carnames and carmodels both with name and id in it.
然后再添加2个包含名称和ID的汽车名称和车型的表格。 now let's say I have carname Mercedes with id 2 and carmodel S classe with id 5, in this case my cartable will contain values 2 and 5 Mercedes S classe.
现在假设我的ID为2的梅赛德斯奔驰汽车,ID为5的S型轿车,在这种情况下,我的购物车将包含值2和5。
To sort them by fields I did this way: 为了按照字段对它们进行排序,我这样做是:
in cars controller: 在汽车控制器中:
@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
and in view 并鉴于
<%= 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"%>
that's all good now. 现在一切都很好。
in your case I just assume it will be something like 在你的情况下,我只是假设它会像
<%= sort_link @search, :company_estimate, "Car make" %> <%= sort_link @search,:company_estimate,“汽车制造”%>
hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.