I was trying to do SUM based mySQL query simliar to the following:
SELECT SUM(updated_on - created_on) AS total from signups
If I were to run this command in the mySQL console I would get a
result. However, if I were to run it using the following command in
Rails:
Signup.find_by_sql("SELECT SUM(updated_on - created_on) AS total from
signups")
The query that is written to the log is:
SELECT SUM(updated_on - created_on) AS total from signups
Like I said before, if I take that query to the mySQL console, I get
the expected result, however the result to the view is simply #
NOW, if take the query and put it in with:
Signup.connection.select_all("SELECT SUM(updated_on - created_on) AS
total from signups")
I get the expected result to the view and the same SQL code is written
to the log
What is the difference then in find_by_sql and connection.select_all
My initial guess is that find_by_sql is expecting a normal find(:all)
based result that follows the model format whereas
connection.select_all allows any sort of SQL code and gives the result
required.
Any thoughts or comments appreciated.
-E