will_paginate和mongodb的问题

时间:2011-01-04 07:41:37

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

堆栈

  1. ruby​​ => 1.9.2头
  2. rails => 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)
  7. 控制台

    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"] 
    

    问题

    第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中,分页应返回=&gt; [“6”,“5”,“4]

    当我更新到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我为了简化而从输出中删除了

1 个答案:

答案 0 :(得分:0)

我发现。附近的内容很好用于跳过

ruby​​-1.9.2-head&gt; 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"]