Rails:将两个不同的模型(表格)与多个标准一起排序

时间:2012-06-01 06:33:17

标签: sql ruby-on-rails

我有两个不同的模型(Container和StockReceipt),我需要合并并命令在表格中显示为交付。我正在对控制器中的记录进行排序:

  

stock_receipts = StockReceipt.where(:delivery_date.gt => 1.month.ago)

     

containers = Container.joins(:shipment).where(:shipment => {:etd.gt => 1.month.ago})

     

@deliveries =(stock_receipts + containers).sort!{| a,b | delivery_date(a)< =>   DELIVERY_DATE(B)}

delivery_date是一个帮助方法,它根据类

返回日期

它有效,但我想按供应商进行分类(在SQL中:ORDER BY delivery_date,supplier_id)。我可以group_by(delivery_date).group_by(供应商),但它有点难看。

我想知道是否有更优雅的方法可以在Rails中或直接在SQL中执行此操作。

谢谢你们, GAM

1 个答案:

答案 0 :(得分:1)

您可以使用Solr和Sunspot gem(http://sunspot.github.com/)等支持多模型搜索的搜索引擎

这是一个小例子:

search = Sunspot.search(Post,Comment) do
  with(:published_at).less_than(Time.now)
  paginate(:page => params[:page])
end