Forum: Ruby on Rails Pagination Question

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.
47e7a0697beaee00ad63009cb747d676?d=identicon&s=25 Binil Thomas (Guest)
on 2006-05-06 15:24
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
4a48822ada395d358a5551f7dd8c90e3?d=identicon&s=25 Bruno Soares (bfs)
on 2006-05-06 21:20
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
47e7a0697beaee00ad63009cb747d676?d=identicon&s=25 Binil Thomas (Guest)
on 2006-05-08 14:29
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
47e7a0697beaee00ad63009cb747d676?d=identicon&s=25 Binil Thomas (Guest)
on 2006-05-08 14:32
(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
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-05-08 16:22
(Received via mailing list)
@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
This topic is locked and can not be replied to.