【问题标题】:Problem with will_paginate and mongodbwill_paginate 和 mongodb 的问题
【发布时间】:2011-01-04 07:41:36
【问题描述】:

堆栈

  1. 红宝石 => 1.9.2 头
  2. 导轨 => 3.0.1
  3. will_paginate => (3.0.pre2)
  4. mongo => (1.1.5)
  5. mongoid => (2.0.0.beta.20)
  6. mongoid_slug => (0.4.6)

控制台

r = Radar.criteria
=> #<Mongoid::Criteria:0x00000104753d48 @selector={}, @options={}, @klass=Radar,     @documents=[]> 
r.map(&:title) 
=> ["1", "2", "3", "4", "5", "6"]

r.active.around(Radius.new,current_location).newest.map(&:title)
=> ["6", "5", "4", "3", "2", "1"] 

问题

第一步

r.active.around(Radius.new,current_location).newest.paginate(:page => 1, :per_page => 3).map(&:title)
=> ["3", "2", "1"] ] 

第 2 步

r.active.around(Radius.new,current_location).newest.paginate(:page => 2, :per_page => 3).map(&:title)
=>["3", "2", "1"]

在 Step1 中分页应该返回 => ["6", "5", "4]

当我将 latest 更改为 most_commented 范围时,一切正常。

r.active.around(Radius.new,current_location).most_commented.map(&:title)
=> ["1", "2", "3", "4", "5", "6"]

r.active.around(Radius.new,current_location).most_commented.paginate(:page =>1 ,:per_page => 3).map(&:title)
=> ["1", "2", "3"]

r.active.around(Radius.new,current_location).most_commented.paginate(:page =>2 ,:per_page => 3).map(&:title)
=> ["4", "5", "6"]

conole 中使用的范围

 scope :most_commented, :order_by => [:comment_count,:desc]
 scope :newest, :order_by => [:created_at,:desc]  

 def self.around(distance,location)
  radius = distance.to_rad
  near(:coords => location.coords + [radius])
 end

ps:我总是在每个示例中使用新的 r=Radar.criteria 我从输出中删除了它以简化

【问题讨论】:

  • 我什么也没看到。你真正的问题是什么?
  • 我更新了我的问题,现在看起来好多了。我在问题段落中描述了我的问题。

标签: mongodb ruby-on-rails-3 mongoid will-paginate


【解决方案1】:

我发现 .near 与 skip 配合得很好

ruby-1.9.2-head > r = Radar.criteria

一切正常

ruby-1.9.2-head > r.active.near(:coords => ul.coords).newest.map(&:title)
=> ["6", "5", "4", "3", "2", "1"] 

相同的查询,但带有 .skip

ruby-1.9.2-head > r.active.near(:coords => ul.coords).newest.skip(2).map(&:title)
=> [] 

第一个使用 .skip 但没有 .near 的查询

ruby-1.9.2-head > r.active.newest.skip(2).map(&:title)
=> ["4", "3", "2", "1"] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2019-09-07
    • 1970-01-01
    • 2012-03-22
    • 2021-02-12
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多