i''ve figured out how to use the next and previous links with the paginator class but now i''m trying to figure out how to display all the page numbers in between. looking through the rails api, i found paginator.each() but i''m not sure how to use it, or if that''s even what i am looking for. also, is there a way to limit the amount of pages like some sites do, where they only show about 10 different page numbers at a time? -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
This two wiki pages should help you: http://wiki.rubyonrails.org/rails/pages/HowtoPagination http://wiki.rubyonrails.com/rails/pages/PaginationHelper Kevin Skoglund> > 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
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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
(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 -- Posted via http://www.ruby-forum.com/.
@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 On 5/8/06, Binil Thomas <binil.thomas.public@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060508/bffaa0c3/attachment.html