Pagination Question


#1

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


#2

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


#3

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


#4

(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


#5

@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