kim
December 19, 2006, 11:48pm
1
I am wondering how to do pagination on a model that has a HABTM
relationship through a join model.
The models
User
has_many :resources
has_many :pages, :through => :resources
…
Page
has_many :resources
has_many :users, :through => :resources
…
Resource
belongs_to :user
belongs_to :page
…
Then in the PageController I have a list method:
def list
@pages = @user.pages # @user is set with a before_filter
end
This works fine - but I want to be able to paginate the @pages list
So how do I do this? I have read all that I can find regarding
pagination and still am not getting it.
Code is always helpful.
Thanks in advance – K
kim
December 20, 2006, 7:16pm
2
Is this impossible or just not challenging enough to get a responce?
Please enlighten me if you know how to do this.
Thanks in advance – K
kim
December 20, 2006, 8:15pm
3
Kim,
Here are 2 links to plug-ins that others have recommended. I tried the
first one about a month ago and never quite got it working (I can’t
remember what the problem was).
Bluehost - Top rated web hosting provider - Free 1 click installs For blogs, shopping carts, and more. Get a free domain name, real NON-outsourced 24/7 support, and superior speed. web hosting provider php hosting cheap web hosting, Web hosting,...
http://codefluency.com/2006/10/24/paginator-released
-Paul
kim
December 20, 2006, 8:31pm
4
On Dec 19, 5:47 pm, “Kim” [email protected] wrote:
Page
def list
Thanks in advance – K
Is the “Custom Pagination” section here helpful?
http://wiki.rubyonrails.org/rails/pages/HowtoPagination
Best,
-r
–
Ryan R.
http://raaum.org
http://rails.raaum.org – Rails docs
http://locomotive.raaum.org – Self contained Rails for Mac OS X
kim
December 20, 2006, 8:41pm
5
The cardboardrocket paginating_find works great for me. Although I had
to make some small changes to get it to work properly… here’s an
example from my code:
@products = Product.find(:all,
:conditions => [ “category_id = ?”, params[:id]],
:order => “name”,
:page => {:size => 20,
:first => 1,
:current => params[:page] || 1 })
Notice the way I handle the :current. anyways hope it helps if you go
that route.
good luck,
Chad
kim
December 20, 2006, 11:02pm
6
Chad - that sounds good. Would you mind sharing a code snippet from
your view as to how you handle the paging? I think that may have been
what I was missing.
Thanks,
Paul
kim
December 20, 2006, 11:46pm
7
Thank you Ryan! The custom pagination worked perfectly.
Here is the code I added to the list method in the controller:
step 1: read and set the variables you’ll need
page = (params[:page] ||= 1).to_i
items_per_page = 8
offset = (page - 1) * items_per_page
step 2: do your custom find without doing any kind of limits or
offsets
@pages = @user.pages
step 3: create a Paginator, the second variable has to be the
number of ALL items on all pages
@which_pages = Paginator.new(self, @pages.length ,
items_per_page, page)
step 4: only send a subset of @pages to the view
@pages = @pages[offset..(offset + items_per_page - 1)]
kim
December 21, 2006, 12:34am
8
On Dec 20, 5:45 pm, “Kim” [email protected] wrote:
@pages = @user.pages
step 3: create a Paginator, the second variable has to be the
number of ALL items on all pages
@which_pages = Paginator.new(self, @pages.length ,
items_per_page, page)
step 4: only send a subset of @pages to the view
@pages = @pages[offset..(offset + items_per_page - 1)]
Mind you, this is not a super efficient method, but as long as the
number of user.pages is not too large it should not impact performance.
Now if users have thousands of pages …
Best,
-r
–
Ryan R.
http://raaum.org
http://rails.raaum.org – Rails docs
http://locomotive.raaum.org – Self contained Rails for Mac OS X