So, I have a Photo model that can be downloaded using full_size and presentation_size . When a user uploads a photo, I track it on the full_downloads and presentation_downloads object.
This is all good.
Sometimes I want to know how much has been downloaded. I have a simple total_downloads method that looks like this:
def total_downloads self.full_downloads + self.presentation_downloads end
My question: I would like to be able to order photos for all three of them (full, presentation, general download). The first two are easy, but how do you place an order for the sum of two columns? Note that this must be both SQLite and PG compatible at a minimum.
A side question: would it be faster to make the total_downloads method a request, and if so, what is the best way to write this? I know to summarize a class that you can call Photo.sum(...) , but I'm not sure how to do this for two columns on the same record.
Thanks!
source share