Forum: Ruby on Rails Paginate from a difficult sql query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
oo00oo (Guest)
on 2006-01-06 20:33
(Received via mailing list)
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
Iván Vega R. (Guest)
on 2006-01-06 22:07
(Received via mailing list)
Wilson B. helped me out with a similar request. Check this site:

http://bigbold.com/snippets/posts/show/389
oo00oo (Guest)
on 2006-01-07 10:26
(Received via mailing list)
Works perfect, thanks !
:)
This topic is locked and can not be replied to.