Pagination with habtm

Hello-

I am building a discussion board-like application in which messages are
associated with one or more workgroups via has_and_belongs_to_many. I
would like to be able to display to my users a distinct list of messages
for the workgroup(s) that they are part of in paginated form. The SQL
for this query would be something like the following:

select distinct (m.id), m.title, m.created_at from messages m,
messages_workgroups mw, workgroups w where m.id = mw.message_id and w.id
= mw.workgroup_id and w.id in (1,2,3)

How can I do something like this using the pagination functionality? I
feel as if I might be missing something simple here.

Thanks.