Rails – How to sort a model on a virtual attribute

[Reading time: 2mn]

Setting
Ruby 1.9.2-p180
Rails 3.0.4

The Problem
So you have a Product model with a couple of virtual attributes like user rating which depends on some clever calculation. It is declared as an instance def in the model, a.k.a: a virtual attribute (ps: remember that class defs – e.g.: def self.do_something – apply to the records as a whole, whereas instance defs – e.g.: def do_something – apply to the current record). But I digress.
Actually you want to sort the products on this user rating but you know that scopes only work at the database level.

Continue reading “Rails – How to sort a model on a virtual attribute”