mongoid 、 will_paginate 、排序不适用于 Mongoid 标准 DSL
根据 github 上的 mongoid 自述文件,我可以做一些奇特的查询,比如 Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).paginate
我尝试与 will_paginate (3.0.pre2) 结合使用
@companies = Company.paginate(:per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
---> ;工作正常
@companies = Company.all.paginate(:per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
--->排序不再起作用
我尝试过
@companies = Company.where(:name=>/^#{params[:search]}/).paginate( :per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
--> 不行了
那就
@companies = Company.paginate(:conditions=>{:name=>/^#{params[:search]}/}, :per_page=>5, :page=>params[:page], :sort => [sort_column, sort_direction])
--->有效
但我认为搜索功能应该在模型中而不是在控制器中! ?
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_direction])
---> 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! ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
不要在分页中使用排序,而是使用
order_by()
。例如:公司型号:
公司控制人(索引):
Solution:
Don't use sort in paginate, use
order_by()
instead. For example:company model:
company controller (index):