Custom, Efficient, Complex Ordering in Rails 3

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

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

In this case, I just pull out the first 10 entries. If I wanted to add an unspecified order of orders, I could create a column in the Model (for example, a custom_ordering column) and enter values ​​for ordering objects. In this case, you simply change the order request ("custom_ordering ASC").

BUT let them say that I want to order based on the user's predictable preference. In this case, it is impossible to create a new column, because each user has different tastes. What will be an effective and efficient way to accomplish this task?

One way would be to create an algorithm for predicting a user assessment of an object from Model called model_rating (object). This algorithm would spit out a positive integer value for their rating. If I went this way, how could I arrange the model?

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

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

But that seems ineffective to me. It should call all the records in the model and then sort them, which, even if you use efficient sorting methods, such as merging or sorting bubbles, will slow performance on models with a lot of records.

Are there any other ways that I am not considering? Can someone point me in the right direction?

+5
source share
2

, , , .

, ( ) .

, , . .

0

Bubble .

, , . , . , . , .

SQL , . .

, . Solr Sunspot. , Sphinx think-sphinx .

0

All Articles