Reputation: 6612
I use will_paginate for the nested notes in the contact show view. The contacts controller looks like this:
@contact = Contact.find(params[:id], :include => :notes)
@notes = @contact.notes.paginate(:page => params[:page], :per_page => 5, :order => "created_at ASC")
The contacts show view has the <%= will_paginate @notes %>
tag. This works, but the sort order is not correct, it shows all 6 notes instead of 5 and the pagination at the top is reversed, it is "next,2,1,previous" instead of "previous,1,2,next"???
Upvotes: 3
Views: 1977
Reputation: 6612
How stupid!! Finally found the problem, the elements had a "float: right" so that caused the reverse order....
Upvotes: 0
Reputation: 514
You supplied :order => "created_at ASC"
to the paginate method instead of the active object you want to have sorted in some way. I could not find the exact point in the will_paginate
gem,
but I bet that the view helper method will_paginate
checks order to see if it goes from left to right or from right to left.
The modified version of Simmo's answer incorporating Dan Seavers suggesion should be correct:
@notes = @contact.notes.order("created_at ASC").paginate(:page => params[:page], :per_page => 5)
Upvotes: 0
Reputation: 1781
You can do this:
@notes = @contact.notes.paginate(:page => params[:page], :per_page => 5, :order => "notes.created_at ASC")
class Contact < ActiveRecord::Base
has_many :notes,:order => "...." #remove this order option if you did
end
class Note < ActiveRecord::Base
default_scope :order => "..." # remove this order option if you did
end
Upvotes: 0
Reputation: 1719
Have you tried moving the ordering into a scope?
You could have a scope in your model like
scope :order_by, lambda { |o| { :order => o } }
And then write your paginate line as
@notes = @contact.notes.order_by("created_at ASC").paginate(:page => params[:page], :per_page => 5)
Upvotes: 3
Reputation: 18979
My first thought was that the :include => :notes
could confuse will_paginate. Have you tried to leave it out?
Upvotes: 0