mongoid , will_paginate , sorting doesnt work with Mongoid criteria DSL
According to the readme of mongoid on github i can do fancy queries like Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).paginate
i tried this in combination with will_paginate (3.0.pre2)
@companies = Company.paginate(:per_page=>5, :page=>params[:page], :sort => [sort_column, sort_dir开发者_StackOverflow社区ection])
---> works fine
@companies = Company.all.paginate(:per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
---> sorting doesnt work anymore
i tried
@companies = Company.where(:name=>/^#{params[:search]}/).paginate( :per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
--> doesnt work
then
@companies = Company.paginate(:conditions=>{:name=>/^#{params[:search]}/}, :per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
---> works
But i think search functions should be in the model not in the controller! ?
Solution:
Don't use sort in paginate, use order_by()
instead. For example:
company model:
def self.search(search)
if !search.blank?
where(:name => /^#{search}/)
else
all
end
end
company controller (index):
@companies = Company.search(params[:search]).order_by([sort_column, sort_direction]).paginate(:per_page=>5, :page=>params[:page])
精彩评论