# HELP - From SQL to Ruby

**URL:** <https://www.ruby-forum.com/t/help-from-sql-to-ruby/116866>\
**Category:** Ruby\
**Created:** [October 10, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/help-from-sql-to-ruby/116866 "2007-10-10T16:21:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luciosan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@luciosan](https://www.ruby-forum.com/u/luciosan)\
**Post date:** [October 10, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/help-from-sql-to-ruby/116866/1 "2007-10-10T16:21:24Z")

</div>

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’)

I’ve all crontrollers and all models

Thank you

---

<div class="post-metadata">

**Author:** ![luciosan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@luciosan](https://www.ruby-forum.com/u/luciosan)\
**Post date:** [October 10, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/help-from-sql-to-ruby/116866/2 "2007-10-10T16:35:34Z")

</div>

On Wed, 10 Oct 2007, luciosan wrote:

> 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 H.

---

<div class="post-metadata">

**Author:** ![luciosan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@luciosan](https://www.ruby-forum.com/u/luciosan)\
**Post date:** [October 13, 2007, 11:15am UTC](https://www.ruby-forum.com/t/help-from-sql-to-ruby/116866/3 "2007-10-13T11:15:41Z")

</div>

In article  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)removed\_email\_address@domain.invalid  
wrote:

> 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?

Thank you for the answare… yes I’m waiting to know how I can do it  
in ActiveRecord.

–

I’m using an evaluation license of nemo since 51 days.  
You should really try it!  
[http://www.malcom-mac.com/nemo](http://www.malcom-mac.com/nemo)
