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?
There’s a couple of things here.
Firstly,
find_by_sqlwill 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:
This will return a number. It’s the same with sums:
You can also use distinction with AR, but it’s not as clean:
In order to query the db directly, this is still possible, and you were almost there: