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 7944571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:35:30+00:00 2026-06-04T00:35:30+00:00

I needed to write a method that always returns an integer value. If the

  • 0

I needed to write a method that always returns an integer value. If the record is non-existent, the method returns 0

I have a unique index on the columns being queried and came up with two solutions:

# Option a) Ruby logic
def some_method id
  result = Model.some_scope.find(:first,
                                  conditions: { foo_id: id },
                                  select: :int_value)

  # Return 0 if record does not exist:
  ( result.nil? ? 0 : result.int_value )
end


# Option b) Allow Postgres to do the work
def some_method id
  # sum() returns 0 if the record wasn't found
  Model.some_scope.where(foo_id: id).sum(:int_value)
end

Because of the unique index, both methods return the same value.

Is there any reason one solution is better than the other, aside from brevity?

My inclination is to choose the solution based on server resources, i.e. minimizing database server load by giving the job to Ruby.


Running Ruby 1.9.3 and Postgres 9.1

  • 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-06-04T00:35:32+00:00Added an answer on June 4, 2026 at 12:35 am

    You don’t need the (possibly more expensive) aggregate function sum().
    Use SQL-COALESCE() for that.

    COALESCE is probably the cheapest way. But the cost is so tiny, it does not matter either way.

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

Sidebar

Related Questions

I have a method that checks certain things and returns a Boolean based on
every night i have a trigger that executes asp.net page with method that first
I want to write a method that gets an object of the same type
Recently I have tackled a strange behaviour of .Net synchronous receive method. I needed
Working with C#. I have an abstract class that I use to read/write settings
According to the docs, super(cls, obj) returns a proxy object that delegates method calls
I needed a method to filter out all elements that are parents of other
I needed to write a script to enter multi-line input to a program (
I needed to update the system tray icon's text value, of my application, whenever
I needed a function that simply checks if a string can be converted to

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.