# Paginate from a difficult sql query

**URL:** <https://www.ruby-forum.com/t/paginate-from-a-difficult-sql-query/51757>\
**Category:** Rails\
**Created:** [January 6, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/paginate-from-a-difficult-sql-query/51757 "2006-01-06T19:33:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 6, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/paginate-from-a-difficult-sql-query/51757/1 "2006-01-06T19:33:40Z")

</div>

Hello,

I have 2 tables

table colors :  
±---------------------+  
| id | user\_id | name |  
±---------------------+  
| 1 | 1 | test1 |  
| 2 | 2 | test2 |  
| 3 | 3 | test3 |  
±---------------------+

table users :  
±---------------------+  
| id | enabled | name |  
±---------------------+  
| 1 | 1 | bob |  
| 2 | 1 | joe |  
| 3 | 0 | mike |  
±---------------------+

I need to get the rows of table colors where the “user\_id” in the table  
user = 1

rows = Color.find\_by\_sql(“SELECT colors.\* FROM colors,users WHERE  
colors.user\_id=users.id AND enabled=1 ORDER BY id DESC”)

So the return is :

±---------------------+  
| id | user\_id | name |  
±---------------------+  
| 2 | 2 | test2 |  
| 1 | 1 | test1 |  
±---------------------+

Everything is good, but I want to have this return directly in a  
paginate. Like :

@paginate\_pages, @colors = paginate :colors,

:per\_page =\> 6,  
:order  
=\> “id DESC”,

:conditions =\> " ??? " ,  
: joins  
=\> " ??? "  
:other  
=\> " ??? "

But I don’t know how to write it. And if it’s possible.

If it’s not possible. How to set the rows array from Color.find\_by\_sql  
into a paginator ?

Can someone help me please ?  
Thanks

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 6, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/paginate-from-a-difficult-sql-query/51757/2 "2006-01-06T21:07:00Z")

</div>

Wilson B. helped me out with a similar request. Check this site:

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 7, 2006, 9:26am UTC](https://www.ruby-forum.com/t/paginate-from-a-difficult-sql-query/51757/3 "2006-01-07T09:26:25Z")

</div>

Works perfect, thanks !  
🙂
