本文介绍了在两个字段中进行排序,其中一个正在创建中,在Rails中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对自定义,高效,Rails 3中的复杂排序

我想为我拥有的Rails模型开发一种有效的订购方法.假设我将所有对象的评级保存在名为"popularity"的字段中.如果我想按此评分排序,我会这样做:

I'd like to develop an efficient ordering method for a rails model I have. Suppose that I save a rating for all objects in a field named "popularity". If I wanted to sort by this rating I would do:

  Model.order('popularity ASC')

如何在创建时按偏斜排序?是否有办法将创建时间戳转换为整数值,然后按受欢迎程度排序-created_at,以便较旧的对象的评级会随着时间的推移而降低? IE之类的东西:

How would I order by a skew for created at? Is there a way to, perhaps, convert the creation timestamp to an integer value, and then sort by popularity - created_at such that older objects' ratings decrease over time? IE something like:

  Model.order('popularity-integerize(created_at) ASC')

所以:我该怎么办?效率高吗?

So: how might I do this, and is it efficient?

推荐答案

在计算受欢迎程度时,您可以考虑创建时间;)

When calculating your popularity, you could just keep the creation time into consideration ;)

这篇关于在两个字段中进行排序,其中一个正在创建中,在Rails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:37