Forum: Ruby on Rails How to run direct sql

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.
chris hulbert (Guest)
on 2006-01-17 06:02
How can i run and get the value from the following sql:

select avg(my_field) from my_table where which_year='0506' and
which_week <= 21 and my_foreign_key_id=10

cheers
Wilson B. (Guest)
on 2006-01-17 06:23
(Received via mailing list)
On 1/16/06, chris hulbert <removed_email_address@domain.invalid> wrote:
> How can i run and get the value from the following sql:
>
> select avg(my_field) from my_table where which_year='0506' and
> which_week <= 21 and my_foreign_key_id=10
>
You can use:
YourModel.connection.select_value(your_query_string)
..which returns the result as a string that you will need to type_cast
into whatever you need.

If you find yourself doing a lot of this, you should look at the cool
Calculations plugin:
http://techno-weenie.net/svn/projects/plugins/calculations/
chris hulbert (Guest)
on 2006-01-17 07:29
thanks!
This topic is locked and can not be replied to.