# Sql query vs Ruby methods

**URL:** <https://www.ruby-forum.com/t/sql-query-vs-ruby-methods/166176>\
**Category:** Rails\
**Created:** [April 22, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/sql-query-vs-ruby-methods/166176 "2009-04-22T15:35:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nike](https://www.ruby-forum.com/u/nike)\
**Post date:** [April 22, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/sql-query-vs-ruby-methods/166176/1 "2009-04-22T15:35:02Z")

</div>

In am using the following code in my application,both the applications  
execute the same result.First one executes fastly than the second  
one,How can i modify the second one since it is fetching all the values,  
from there it is limiting 5 rather than the first one

User.all.find(:all,:conditions=\>[“id not  
in(?)”,Person.all],:limit=\>5,:order=\>‘created\_at DESC’)

User.all.find(:all,:order=\>‘created\_at DESC’).delete\_if{|x| x id  
Person.all.include?(x)}[0…4]

---

<div class="post-metadata">

**Author:** ![nike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nike](https://www.ruby-forum.com/u/nike)\
**Post date:** [April 22, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/sql-query-vs-ruby-methods/166176/2 "2009-04-22T15:48:25Z")

</div>

Really both quieries look crazy, and will perform poorly on any  
realistic dataset.

I guess Person is a subclass of a User, so if you are using STI just  
try:

User.all(:conditions =\> “type !=  
‘Person’”, :limit=\>5, :order=\>‘created\_at DESC’)

Dmitry

---

<div class="post-metadata">

**Author:** ![nike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nike](https://www.ruby-forum.com/u/nike)\
**Post date:** [April 22, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/sql-query-vs-ruby-methods/166176/3 "2009-04-22T16:18:00Z")

</div>

Dmitry S. wrote:

> Really both quieries look crazy, and will perform poorly on any  
> realistic dataset.
> 
> I guess Person is a subclass of a User, so if you are using STI just  
> try:
> 
> User.all(:conditions =\> “type !=  
> ‘Person’”, :limit=\>5, :order=\>‘created\_at DESC’)
> 
> Dmitry

Thanks
