Forum: Ruby on Rails How to get results from database SQL query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0e2011a2e6e23e05262397c76ef4dcb?d=identicon&s=25 Yan Oleg (yan-oleg)
on 2008-11-18 16:23
I need to get results from SQL query to database.

I have an SQL statement, e.g.: SELECT MAX(my_field) max_my_field FROM
my_table WHERE id=5

I want to get the result, what is the max value. I have to get only one
record with a number, or null if no records found. Please provide Ruby
code to do this? (to store results in variable)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-18 17:03
(Received via mailing list)
On Nov 18, 3:23 pm, Yan Oleg <rails-mailing-l...@andreas-s.net> wrote:
> I need to get results from SQL query to database.
>
> I have an SQL statement, e.g.: SELECT MAX(my_field) max_my_field FROM
> my_table WHERE id=5
>
> I want to get the result, what is the max value. I have to get only one
> record with a number, or null if no records found. Please provide Ruby
> code to do this? (to store results in variable)

Checkout the maximum method (http://api.rubyonrails.com/classes/
ActiveRecord/Calculations/ClassMethods.html#M001659)

Fred
This topic is locked and can not be replied to.