Need translation of sql query to :find equivalent


#1

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?


#2

Brad S. 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.’


#3

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.


#4

On 6 Oct 2008, at 16:41, Brad S. 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 %>