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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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). http://cardboardrocket.com/pages/paginating_find http://codefluency.com/2006/10/24/paginator-released -Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-20 19:29 UTC
Re: Pagination with HABTM relationship
On Dec 19, 5:47 pm, "Kim" <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 -- KIs the "Custom Pagination" section here helpful? http://wiki.rubyonrails.org/rails/pages/HowtoPagination Best, -r -- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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)]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-20 23:33 UTC
Re: Pagination with HABTM relationship
On Dec 20, 5:45 pm, "Kim" <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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)]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 Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---