Forum: Ruby on Rails Need translation of sql query to :find equivalent

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.
47e90aff63339b93872ddf8431dfc35c?d=identicon&s=25 Bradley Symons (snomys)
on 2008-10-06 16:24
I have this query, I have tried a few permutations to translate it into
a :find equivalent, as a newbie this is a bit trickier than i first
envisaged.

heres the core sql:
select resulttype from results where
id in
(
select result_id from outcomes o1 where
o1.outcome_date = (select max(o2.outcome_date) from outcomes o2 where
o2.testcase_id = o1.testcase_id
and o1.testcase_id ='1'));

can you give me a few pointers, does it require a join clause from the
results table to the outcomes, or can rails do this easier?
C4bfcc81ac9281cb905f38e97e4d4e0b?d=identicon&s=25 Shandy Nantz (snantz)
on 2008-10-06 17:33
Brad Symons wrote:
Try

@result = Resulttypes( :all, :conditions => ['id in ( select result_id
from outcomes o1 where o1.outcome_date = (select max(o2.outcome_date)
from outcomes o2 where o2.testcase_id = o1.testcase_id and
o1.testcase_id =\'1\'))']

This should work, I am doing seomthing similiar only I am say 'NOT IN.'
47e90aff63339b93872ddf8431dfc35c?d=identicon&s=25 Bradley Symons (snomys)
on 2008-10-06 17:41
i cannot get the brackets right, you seem to have more brackets on the (
than on the ), when i mess around with this it just throws all kinds of
errors.

here is another query i am trying to write using :find

Result.find(:all, :conditions => 'resulttype = (select resulttype from
results where id = 1)')

when i run this with debug

<%= debug Result.find(:all, :conditions => 'resulttype = (select
resulttype from results where id = 1)') %>

i get:
resulttype = 'pass'
id = '1'

but when i remove debuy, i just get ####, adding a h, like this
<%= h Result.find(:all, :conditions => 'resulttype = (select resulttype
from results where id = 1)') %>

i get #<Result:0x395f080>

can anyone point me to a good source to understand more about the :find,
because its quite obscure on the rails reference.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-06 17:59
(Received via mailing list)
On 6 Oct 2008, at 16:41, Brad Symons wrote:

>

> <%= h Result.find(:all, :conditions => 'resulttype = (select
> resulttype
> from results where id = 1)') %>
>
> i get #<Result:0x395f080>
>

<%= %> just calls to_s on the result inside it. The to_s on an array
is just the concatenation of the result of to_s on the elements of the
array, and the default to_s on an AR object is unhelpful. It's up to
you do to something like

<% @some_results.each do |result|
   date: <%= result.created_at %>
   value: <%= result.value %>
...
<% end %>
This topic is locked and can not be replied to.