# Pagination Question

**URL:** <https://www.ruby-forum.com/t/pagination-question/94546>\
**Category:** Rails\
**Created:** [May 6, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/pagination-question/94546 "2006-05-06T15:24:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Binil\_Thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@Binil\_Thomas](https://www.ruby-forum.com/u/Binil_Thomas)\
**Post date:** [May 6, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/pagination-question/94546/1 "2006-05-06T15:24:00Z")

</div>

I am new to Rails and am running into a problem with pagination.  
Consider model classes Employee and Department:

class Employee \< ActiveRecord::Base  
belongs\_to :department  
end

class Department \< ActiveRecord::Base  
has\_many :employees  
end

In my controller, I need to get a list of employees working in a given  
department, and I need to paginate this list. Any pointers on how to  
acheive this?  
def list\_employees  
department\_name = “My Department”  
@employees = # … ??  
@employee\_pages = # …??  
end

Thanks,  
Binil

---

<div class="post-metadata">

**Author:** ![Binil\_Thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@Binil\_Thomas](https://www.ruby-forum.com/u/Binil_Thomas)\
**Post date:** [May 6, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/pagination-question/94546/2 "2006-05-06T21:20:09Z")

</div>

Hey!

Think you can pass a set of conditions to your paginator.

For example in my work if I’m looking for pages of a book with a certain  
ID,  
I just do:

@page\_pages, @pages =  
paginate :pages, :order\_by =\> ‘position’,  
:per\_page =\> ITEMSINLIST, :parameter =\>  
‘pagelist’,  
:conditions =\> ["book\_id = ? ", id\_im\_searching\_for]

Hope this helps,

Bruno Soares

---

<div class="post-metadata">

**Author:** ![Binil\_Thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@Binil\_Thomas](https://www.ruby-forum.com/u/Binil_Thomas)\
**Post date:** [May 8, 2006, 2:29pm UTC](https://www.ruby-forum.com/t/pagination-question/94546/3 "2006-05-08T14:29:50Z")

</div>

Binil Thomas wrote:

> In my controller, I need to get a list of employees working in a given  
> department, and I need to paginate this list. Any pointers on how to  
> acheive this?

I did it using something like:

def list\_employees  
department\_name = “My Department”  
count = Employee.count\_by\_sql([“select count(\*) from employees  
where employee.department\_id in  
(select id from department where department.name=?)”,  
department\_name])  
# 10 employees listed on a page  
@employee\_pages = Paginator.new self, count, 10, @params[‘page’]  
@employees = Employee.find\_by\_sql([“select \* from employees  
where employee.department\_id in  
(select id from department where department.name=?)  
order\_by created\_at desc limit ? offset ?”,  
department\_name, @employee\_pages.items\_per\_page,  
@employee\_pages.current.offset])  
end

I was hoping that ActiveRecord offers some help in writing that  
counter/finder query pair. Does anyone know of any such feature?

Thanks,  
Binil

---

<div class="post-metadata">

**Author:** ![Binil\_Thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@Binil\_Thomas](https://www.ruby-forum.com/u/Binil_Thomas)\
**Post date:** [May 8, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/pagination-question/94546/4 "2006-05-08T14:32:12Z")

</div>

(Fixed typos in the code)  
Binil Thomas wrote:

> In my controller, I need to get a list of employees working in a given  
> department, and I need to paginate this list. Any pointers on how to  
> acheive this?

I did it using something like:

```
def list_employees
  department_name = "My Department"
  count = Employee.count_by_sql(["select count(*) from employees
             where department_id in
               (select id from department where department.name=?)",
             department_name])
  # 10 employees listed on a page
  @employee_pages = Paginator.new self, count, 10, @params['page']
  @employees = Employee.find_by_sql(["select * from employees
             where department_id in
               (select id from department where department.name=?)
             order_by created_at desc limit ? offset ?",
             department_name, @employee_pages.items_per_page,
             @employee_pages.current.offset])
end

```

I was hoping that ActiveRecord offers some help in writing that  
counter/finder query pair. Does anyone know of any such feature?

Thanks,  
Binil

---

<div class="post-metadata">

**Author:** ![Binil\_Thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@Binil\_Thomas](https://www.ruby-forum.com/u/Binil_Thomas)\
**Post date:** [May 8, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/pagination-question/94546/5 "2006-05-08T16:22:39Z")

</div>

@employee\_pages, @employees = paginate :employees, :per\_page =\> 10,  
:include =\> “department”, :conditions =\> [“department.name =  
?”,department\_name]

@employee\_pages will be the Paginator object, so  
@employee\_pages.item\_count will tell you how many total employees there  
are
