Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:23:17+00:00 2026-05-24T17:23:17+00:00

So, I have a Photo model which can be downloaded at full_size and presentation_size

  • 0

So, I have a Photo model which can be downloaded at full_size and presentation_size. When a user downloads a photo I track this on the photo’s full_downloads and presentation_downloads attribute.

That’s all good.

Sometimes I want to know how many total downloads there have been. I have a simple method, total_downloads which looks like so:

def total_downloads
  self.full_downloads + self.presentation_downloads
end

My question is: I would like to be able to order photos by all three of these (full, presentation, total downloads). The first two are easy, but how do you do an order by the sum of two columns? Note this needs to be both SQLite and PG compatible at minimum.

A side question, would it be faster to make the total_downloads method a query, and if so what’s the best way to write that? I know to sum on the class you can call Photo.sum(...), but I’m not sure how to do that for two columns on a single record.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:23:19+00:00Added an answer on May 24, 2026 at 5:23 pm

    You can try this:

    Photo.order('full_downloads + presentation_downloads')
    

    It will run this SQL query:

    SELECT "photos".* FROM "photos" ORDER BY full_downloads + presentation_downloads
    

    This is potentially slow though. If you have a large dataset and use this sort order often, you should consider creating a total_downloads column and recalculating its value if the record’s full_downloads or presentation_downloads column changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a notices model which contains records of notices for each user. Each
I have a micropost form which allows a user to upload a photo and
So I have a Photo model of which the first photo that belongs to
I have a Photo model and I want to set up a form so
Background: I have an MVC based polaroid object. The model keeps the photo's metadata,
I have a photo gallery I would like that every time a user does
So we have a photo like this (source: vkontakte.ru ) How to detect that
So, I have the following scope in my Photo model: scope :best, order(:average_rating.desc) The
I have a simple view in which I am trying to access the Model
I have the models Home and Photo, which have a has_many - belongs_to relationship

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.