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.