On Wed, 10 Oct 2007, luciosan wrote:

> Hi
>
> How I can execute this query on Ruby?
>
> SELECT *
> FROM table_1 T
> WHERE T.document_id not in (SELECT distinct documnet_id FROM
> binary_files);
>
> Now, I execute without proble the second part of query (SELECT
> distinct documnet_id FROM binary_files) in this
> method:BinaryFile.find(:first, :select => 'distinct document_id')

dbh = DBI.open('dbi:mysql:foo','username','password)
dbh.select_all('SELECT * FROM table_1 T WHERE T.document_id not in (SELECT 
distinct documnet_id FROM binary_files)') do |row|
   # do stuff with rows
end

or, maybe

ksdbh = KSDatabase.new('dbi:mysql:foo','username','password)
doc_ids = ksdbh.select(:BinaryFiles) do |b|
   b.distinct(b.document_id)
end.collect do |d|
   d.document_id
end

data = ksdbh.select(:Table1) {|t| t.document_id.not_in(doc_ids)}

> I've all crontrollers and all models

My point is that there are many ways to do what you ask in _Ruby_.  I 
imagine you are actualy wanting to know how to do it in ActiveRecord?


Kirk Haines