在Rails 3中进行自定义,高效,复杂的排序 [英] Custom, Efficient, Complex sorting in Rails 3

查看:107
本文介绍了在Rails 3中进行自定义,高效,复杂的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Rails中高效地订购.我们都知道,您可以使用以下方法在Rails中进行简单的订购:

Model.order("created_at ASC").limit(10)

在这种情况下,我只是提取前10个条目.如果要添加非时间限制的排序,可以在模型中创建一列(例如custom_ordering column)并输入值以对对象进行排序.在这种情况下,您只需修改order("custom_ordering ASC")调用.

但是,假设我要根据用户的预测偏好进行订购.在这种情况下,创建新列是不可行的,因为每个用户都有不同的喜好.一种聪明,有效的方式来完成这项任务?

一种方法是创建一种算法,以根据名为model_rating(object)的模型预测用户对某个对象的评级.该算法将给出其评分的正整数值.如果我走这条路,我将如何有效地订购模型?

另一种方法是将算法设置为model_entry.user_preference(user).然后我可以按以下顺序订购:

Model.all.sort! { |b, a| a.user_preference(user) <=> b.user_preference(user) } 

但这使我感到无能为力.它必须调用模型中的所有条目,然后对它们进行排序,即使您使用有效的排序方法(例如合并或冒泡排序),在具有大量条目的模型上也会表现不佳.

我还没有考虑其他途径吗?有人可以在这里向我指出正确的方向吗?

解决方案

在此解决方案中,我假设用户的首选订单的计算成本很高,并且您订购的商品集不会经常更新. >

您可以做的是经常(在后台任务中)计算订单,并将计算的订单存储在每个用户的单独表中.

或者,如果您没有很多用户,则可以将每个用户每个模型的得分保存在单独的表中,然后在联接中按该值排序.然后可以定期重新计算这些值.

I would like to know how to order efficiently in Rails. We all know that you can do simple ordering in Rails with the following methods:

Model.order("created_at ASC").limit(10)

In this case I'm just pulling the first 10 entries. If I wanted to add a non-time delineated ordering I could create a column in the Model (e.g. custom_ordering column) and input values to order the objects. That case you would just modify the order("custom_ordering ASC") call.

BUT let's say I want to order based off a user's predicted preference. In this case, it's not feasible to create a new column because each user has different tastes. What would be a smart, efficient way to accomplish this task?

One method would be to create an algorithm to predict a user's rating of an object from Model called model_rating(object). This algorithm would spit out a positive integer value of their rating. If I went down this path, how would I efficiently order the Model?

Another method would be to set the algorithm up as a model_entry.user_preference(user). Then I could order by:

Model.all.sort! { |b, a| a.user_preference(user) <=> b.user_preference(user) } 

But this strikes me as inefficient. It has to call all the entries in the model and then sort them which, even if you use efficient sorting methods like merge or bubble sort, gets poor performance on models with large numbers of entries.

Are there other paths I am not considering? Can anyone point me in the right direction here?

解决方案

In this solution I'm assuming that the calculation of the user's preferred order is expensive and that the set that you are ordering is not updating to often.

What you could do is calculate the order every so often (in a background task) and store the calculated order in a separate table per user.

Alternatively, if you have not a lot of users, you could save in a separate table the score of each model per user, and sort by that value in a join. These values can then also be recalculated on a regular basis.

这篇关于在Rails 3中进行自定义,高效,复杂的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆