2013-11-15 25 views
0

以下函数通过投票降序(使用活动记录信誉系统link to gem)对企业(关系)进行排序。find_with_reputation的多个订单值

@businesses = Business.find_with_reputation(:votes, :all, order: "votes desc") 

如何将添加次要顺序值,以便它按票,但如果票数相等它按created_at(最早在顶部)?

这纯粹是:

@businesses = Business.find_with_reputation(:votes, :all, order: "votes desc, created_at desc") 
+0

虽然我不知道“find_with_reputation”是什么,但看起来是正确的。 – Zippie

回答

1

我怀疑你提出的答案会导致SQL错误,因为“created_at”可能会出现在这两个声誉表和业务表。为了解决这个问题,我建议在created_at上进行具体化。

Business.find_with_reputation(:votes, :all, order: "votes desc, businesses.created_at desc") 

但是,我会建议您使用ID列。数据库的INT会更快,排序顺序应该相同。

Business.find_with_reputation(:votes, :all, order: "votes desc, businesses.id desc") 
+0

感谢您的回答。我会试一下 – grabury