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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:31:47+00:00 2026-05-29T06:31:47+00:00

My rails 3 app needs to use a SELECT DISTINCT which (as far as

  • 0

My rails 3 app needs to use a SELECT DISTINCT which (as far as I know) cannot be done with activerecord queries. So I have been executing direct SQL and it is running fine locally on sqllite — But it is failing at Heroku (postgres).

In my local (sqllite) app, this works fine:

r = ActiveRecord::Base.connection.execute("my query string")

But on heroku, using ActiveRecord::Base.connection.execute ALWAYS returns an empty dataset

#<PGresult:0x0000000xxxxxxxxx>

even for very simple queries such as

r = ActiveRecord::Base.connection.execute("SELECT numeric_score  FROM beeps WHERE store_id = '132' AND survey_num = '2'")

So I’m using heroku console to debug some very basic SQL queries to try to understand how to re-format my SQL to work at Heroku/Postgres.

SELECT column_name WORKS: the heroku console, selecting postgres records is no problem, for example this works fine:

n = Beep.find_by_sql("SELECT numeric_score  FROM beeps WHERE store_id = '132' AND survey_num = '2'")

gives the three values expected:

[#<Beep numeric_score: 10>, #<Beep numeric_score: 9>, #<Beep numeric_score: 8>]

But SELECT COUNT fails?? When I try to COUNT them in the SQL

n = Beep.find_by_sql("SELECT COUNT(*)  FROM beeps WHERE store_id = '132' AND survey_num = '2'")

it fails, giving:

[#<Beep >]

And SELECT SUM(column) fails too?? When I try to SUM them

n = Beep.find_by_sql("SELECT SUM(numeric_score)  FROM beeps WHERE store_id = '132' AND survey_num = '2'")

it also fails, giving:

[#<Beep >]

How do I execute direct SQL with Postgres… SUM(columnname) and COUNT(*) should work, right?

  • 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-29T06:31:48+00:00Added an answer on May 29, 2026 at 6:31 am

    There’s a couple of things here.

    Firstly, find_by_sql will return initialised objects based on the data coming back, which is why you’re not seeing anything coming back from your counts.

    In order to do this with AR you can do:

    Beep.where(:store_id => 123).where(:survey_num => 2).count
    => 5
    

    This will return a number. It’s the same with sums:

    Beep.where(:store_id => 123).where(:survey_num => 2).sum(:numeric_score)
    => 5
    

    You can also use distinction with AR, but it’s not as clean:

    Beep.select("DISTINCT *").where(:store_id => 123).where(:survey_num => 2)
    => [<Beep>, <Beep>...etc]
    

    In order to query the db directly, this is still possible, and you were almost there:

    conn = ActiveRecord::Base.connection
    sql = "SELECT DISTINCT * FROM beeps WHERE store_ID = 123"
    res = conn.execute sql
    
    # res is now a PGResult object
    
    res.each do |row|
      puts row["id"]
      puts row["numeric_score"]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app that needs to expose values from a database as
I have a MySQL/Rails app that needs search. Here's some info about the data:
I have a ruby on rails app running a server and sometimes it needs
I have a silverlight application that needs to talk to a rails app to
I have a gallery in my rails app that needs to only allow certain
Let's say in a Rails app you have some gems that you use in
I have a rails app which uses omniauth to authenticate visitors via facebook. When
I have a Rails 3 web application which needs to: retrieve a stream data
We're working on a Ruby on Rails app that needs to take advantage of
I am writing my first rails app. It needs to aggregate some data from

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.