Will_Paginate Can It Order by Day

will_paginate can it order by day

You cannot use will paginate by group/scope by date. You'll need to do this yourself. It's not terribly difficult though, you could do something like this:

# pictures_controller.rb

def index
@date = Date.parse(params[:date]) rescue Date.today
@pictures = Picture.where(:created_at => @date.at_midnight..@date.next_day.at_midnight)

@total_pictures = Picture.count
@current_pictures = @pictures.size
end

# pictures/index.html.haml

- @pictures.each do |picture|
= #....

= "Showing #{@current_pictures} pictures for #{@date}."
= "There are a total of #{@total_pictures} pictures."

= link_to 'View Previous day photos', pictures_url(:date => @date.prev_day)
- if @date.past?
= link_to 'View Next day photos', pictures_url(:date => @date.next_day)

Ruby on Rails Pagination: Sort order not respected (will_paginate gem)

I was able to solve the problem after answering some other user's questions. After reviewing my "Photo" model, at the request of @mislav I realized that it contained:

default_scope :order => 'date'

After removing this default_scope, ordering started functioning properly as it should. Problem solved.

Will_Paginate and order clause not working

If you look at the example from the will_paginate github page you can spot one important difference between their use of the :order clause and yours:

@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'

This calls paginate on the Post object (with no objects being selected yet - no SQL has been executed before paginate comes along). This is different in your example: as you state in the first line of code "returns an array of articles". The simplest I can come up with showing the problem is

  results = Model.limit(5).all
@results = results.paginate :order => :doesnt_matter_anymore

won't sort, but this will:

  results = Model.limit(5)
@results = results.paginate :order => :matters

It should suffice to take the all out of the search method. It makes ActiveRecord actually perform the SQL query when calling this method. Will_paginate will do that for you when you call paginate (if you let it...). Check out the section on Lazy Loading in this post about Active Record Query Interface 3.0

will_paginate limit and order not working

How stupid!! Finally found the problem, the elements had a "float: right" so that caused the reverse order....

will_paginate limit and order not working

How stupid!! Finally found the problem, the elements had a "float: right" so that caused the reverse order....

rails ,paginate, sort and more than one table

I think the Kaminari gem is a little bit better now than will_paginate. Take a look at that one too.

Rails hash to will_paginate format

will_paginate can make use of plain arrays instead of ActiveRecord scopes.

You just need to treat your data before. There is plenty of resource on how to do it:

https://makandracards.com/makandra/13521-will_paginate-can-paginate-plain-ruby-arrays

https://gist.github.com/denmarkin/817327



Related Topics



Leave a reply



Submit