下面的函数按投票降序(使用活动记录声誉系统链接到宝石)对业务(关系)进行排序。
@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")发布于 2013-11-15 14:59:59
我怀疑您提出的答案会导致SQL错误,因为'created_at‘可能同时出现在信誉表和业务表中。为了避免这种情况,我建议在created_at上做一些具体的说明。
Business.find_with_reputation(:votes, :all, order: "votes desc, businesses.created_at desc")但是,我建议您使用id列。DBs在INT中会更快,排序顺序应该是相同的。
Business.find_with_reputation(:votes, :all, order: "votes desc, businesses.id desc")https://stackoverflow.com/questions/20001246
复制相似问题