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.
B9f2cd54b3f85455af152162c2e5b1b2?d=identicon&s=25 chris hulbert (Guest)
on 2006-01-17 05: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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-17 05:23
(Received via mailing list)
On 1/16/06, chris hulbert <chris.hulbert@ruralpress.com> 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/
B9f2cd54b3f85455af152162c2e5b1b2?d=identicon&s=25 chris hulbert (Guest)
on 2006-01-17 06:29
thanks!
This topic is locked and can not be replied to.