检索双倍指定的限制

时间:2015-03-13 16:46:37

标签: javascript ruby-on-rails ajax activerecord

我遇到这个奇怪的问题,我在客户端(JS)设置了一个限制(和偏移),通过ajax将它发送到Rails控制器,即使params说限制是我期望的(14) ),吐出双倍(28)。有一个'查看更多'按钮,它比另一组轨道分页并上升偏移,再次应该是14,表示它的14,但是吐了一个额外的28.我记录一些put语句,输出就是我的expect(限制14,每次分页时偏移量增加14)。关于为什么我得到双倍限制的任何想法我指定的?这是一些codez:

以下是相关的JS函数:

 function queue(params, method) {
    var req;
    params = params || {};
    params = _.extend(params, {
      limit: 14,
      order: _order
    });
    method = method || 'html';
    req = {
      params: params,
      method: method
    };

    fetch(req);
  }

  /**
   * Hits our filter_tracks controller action
   * @private
   */
  function fetch(req){
    self.spin(true);
    if (mm.isMobile) {
      mm.spin(true);
    }

    if(_fetchingAjax) _fetchingAjax.abort();
    _fetchingAjax = $.ajax({
      url: '/filter_tracks',
      data: req.params,
      success: function (data) {
        $resultsTarg[req.method](data);
        buildTracks();
        mm.facade.trigger('bypass');
        self.spin(false);
        if (mm.isMobile) {
          mm.spin(false);
        }
        if (mm.isMobile && !req.params.offset) {
          self.view(false);
        }
        if (!req.params.offset) _.defer(count);
      }
    }).always(function(){
      _fetchingAjax = null;
    });
  }

  /**
   * Tracks must be built (instantiated) every time the partial is rerendered or
   * becomes extended with the 'view more' button
   * @private
   */
  function buildTracks() {
    var tracks = $('.track-item', $resultsTarg);
    // if tracks already exist, remove them [MAKE ME BETTER]
    if (_tracks.length) {
      _tracks.forEach(function (track) {
        track.destroy();
      });
      _tracks = [];
    }
    // recreate tracks
    tracks.each(function (i, track) {
      _tracks.push(mm.Track($(track)));
    });
  }

它出现在我的轨道控制器中:

def filter_tracks

    params[:limit] ||= 50
    params[:offset] ||= 0
    params[:order] ||= 'rank DESC'

    puts "/" * 150
    puts params[:limit]
    puts params[:offset]
    puts "/" * 150


    # heavy rotation filter flag
    heavy_rotation ||= (params[:order] == 'rank DESC')


    @result_offset = params[:offset]
    @tracks = Track.ready.with_artist

    params[:order] = "tracks.#{params[:order]}"

    if params[:order]
      order = params[:order]
      order.match(/artist.*/){|m|
        params[:order] = params[:order].sub /tracks\./, ''
      }
      order.match(/title.*/){|m|
        params[:order] = params[:order].sub /tracks.(title)(.*)/i, 'LOWER(\1)\2'
      }
    end


    @tracks = parse_params(params[:q], @tracks)

    @tracks = @tracks.offset(params[:offset])

    @result_count = @tracks.count

    @tracks = @tracks.order(params[:order], 'tracks.updated_at DESC').limit(params[:limit])

    # restructure heavy rotation results
    if heavy_rotation

      puts "*" * 150
      puts params[:limit]
      puts params[:offset]
      puts "*" * 150

      three_weeks_ago = Time.now - 21.days

      # sub-query top results
      tracks_top_results = @tracks.where(
        "tracks.updated_at >= :top", 
        top: three_weeks_ago
      )

      # sub-query remaining
      tracks_remaining_results = @tracks.where(
        "tracks.updated_at < :remaining", 
        remaining: three_weeks_ago
      )

      @tracks = tracks_top_results + tracks_remaining_results
      @tracks.shuffle!


    end

    render partial: "shared/results"
  end
  

编辑:

这是日志:

2015-03-16T17:06:27.637654+00:00 app[web.2]: Started GET "/browse?_ajax=1" for 198.0.42.137 at 2015-03-16 17:06:27 +0000
2015-03-16T17:06:27.748603+00:00 app[web.2]:   Rendered shared/_results.html.erb (0.9ms)
2015-03-16T17:06:27.659777+00:00 app[web.2]: Processing by TracksController#index as */*
2015-03-16T17:06:27.659818+00:00 app[web.2]:   Parameters: {"_ajax"=>"1"}
2015-03-16T17:06:27.745758+00:00 app[web.2]:   Rendered shared/_sorter.html.erb (0.7ms)
2015-03-16T17:06:27.753180+00:00 app[web.2]:   Rendered shared/_table.html.erb (6.5ms)
2015-03-16T17:06:27.753270+00:00 app[web.2]:   Rendered tracks/index.html.erb (40.1ms)
2015-03-16T17:06:27.753674+00:00 app[web.2]: Completed 200 OK in 94ms (Views: 78.4ms | ActiveRecord: 3.8ms)
2015-03-16T17:06:27.769464+00:00 heroku[router]: at=info method=GET path="/browse?_ajax=1" host=staging-marmosetmusic.herokuapp.com request_id=846db817-4654-43ab-8f99-7994b8a18950 fwd="198.0.42.137" dyno=web.2 connect=1ms service=136ms status=304 bytes=378
2015-03-16T17:06:27.960153+00:00 app[web.2]: Started GET "/moods.json?bucket=story" for 198.0.42.137 at 2015-03-16 17:06:27 +0000
2015-03-16T17:06:28.188416+00:00 app[web.2]: Started GET "/filter_tracks?limit=14&order=rank+DESC" for 198.0.42.137 at 2015-03-16 17:06:28 +0000
2015-03-16T17:06:28.210975+00:00 app[web.2]: Processing by TracksController#filter_tracks as */*
2015-03-16T17:06:28.211023+00:00 app[web.2]:   Parameters: {"limit"=>"14", "order"=>"rank DESC"}
2015-03-16T17:06:28.222862+00:00 app[web.2]: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2015-03-16T17:06:28.222866+00:00 app[web.2]: 14
2015-03-16T17:06:28.222868+00:00 app[web.2]: 0
2015-03-16T17:06:28.222870+00:00 app[web.2]: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2015-03-16T17:06:28.318029+00:00 app[web.2]: ******************************************************************************************************************************************************
2015-03-16T17:06:28.318037+00:00 app[web.2]: 14
2015-03-16T17:06:28.318039+00:00 app[web.2]: 0
2015-03-16T17:06:28.318041+00:00 app[web.2]: ******************************************************************************************************************************************************
2015-03-16T17:06:28.348873+00:00 app[web.2]: Processing by TracksController#moods as JSON
2015-03-16T17:06:28.348884+00:00 app[web.2]:   Parameters: {"bucket"=>"story"}
2015-03-16T17:06:28.478141+00:00 heroku[router]: at=info method=GET path="/moods.json?bucket=story" host=staging-marmosetmusic.herokuapp.com request_id=b68356eb-7b2f-4341-ba30-29d6d3e4ff5d fwd="198.0.42.137" dyno=web.2 connect=1ms service=527ms status=304 bytes=378
2015-03-16T17:06:28.463840+00:00 app[web.2]: Completed 200 OK in 115ms (ActiveRecord: 8.0ms)
2015-03-16T17:06:28.710111+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (115.9ms)
2015-03-16T17:06:28.463520+00:00 app[web.2]: Read fragment views/staging-marmosetmusic.herokuapp.com/moods.json (1.4ms)
2015-03-16T17:06:28.593527+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (10.0ms)
2015-03-16T17:06:28.714491+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.7ms)
2015-03-16T17:06:28.720260+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.7ms)
2015-03-16T17:06:28.725627+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.5ms)
2015-03-16T17:06:28.717344+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.6ms)
2015-03-16T17:06:28.722971+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.5ms)
2015-03-16T17:06:28.728223+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.4ms)
2015-03-16T17:06:28.734289+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.1ms)
2015-03-16T17:06:28.741642+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.3ms)
2015-03-16T17:06:28.748142+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.9ms)
2015-03-16T17:06:28.758640+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.9ms)
2015-03-16T17:06:28.758645+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.2ms)
2015-03-16T17:06:28.730928+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.5ms)
2015-03-16T17:06:28.737758+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.1ms)
2015-03-16T17:06:28.744971+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.0ms)
2015-03-16T17:06:28.752438+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (4.0ms)
2015-03-16T17:06:28.765492+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.9ms)
2015-03-16T17:06:28.773917+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.0ms)
2015-03-16T17:06:28.780813+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.4ms)
2015-03-16T17:06:28.788779+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.1ms)
2015-03-16T17:06:28.796253+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.1ms)
2015-03-16T17:06:28.762271+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.7ms)
2015-03-16T17:06:28.770673+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (4.9ms)
2015-03-16T17:06:28.778036+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.8ms)
2015-03-16T17:06:28.785552+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (4.0ms)
2015-03-16T17:06:28.792803+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (3.7ms)
2015-03-16T17:06:28.799688+00:00 app[web.2]:   Rendered shared/_track_item.html.erb (2.9ms)
2015-03-16T17:06:28.799694+00:00 app[web.2]:   Rendered shared/_results.html.erb (219.2ms)
2015-03-16T17:06:28.799893+00:00 app[web.2]: Completed 200 OK in 589ms (Views: 222.2ms | ActiveRecord: 170.6ms)
2015-03-16T17:06:28.834891+00:00 heroku[router]: at=info method=GET path="/filter_tracks?limit=14&order=rank+DESC" host=staging-marmosetmusic.herokuapp.com request_id=ebb74235-f5a5-4501-ab52-7857ef93db5d fwd="198.0.42.137" dyno=web.2 connect=1ms service=649ms status=200 bytes=5515

0 个答案:

没有答案