Why cant I do this in my controller-Collection of common programming errors

def search
  @location = Location.find(params[:location_id])
  start_date = DateTime.strptime(params[:start_date], "%m-%d-%Y")
  end_date = DateTime.strptime(params[:end_date], "%m-%d-%Y")
  @songs = @location.songs.find(:all, :conditions => {:play_date => start_date..end_date}).paginate(:page => params[:page], :per_page => 40)
  render 'show'
end

Here is my error

undefined method `paginate' for #

all works if i remove the will_paginate but i need it…any ideas or is there a better way to write this controller

  1. Try writing

    @songs = @location.songs.where(:play_date => start_date..end_date).paginate(:page => params[:page], :per_page => 40)
    

    The difference? where returns an ActiveRelation object, while find retrieves all the matching objects in an array.

    Hope this helps.

  2. NoMethodError: undefined method `paginate' for []:Array
    

    My will_paninate works perfectly but above error jumped out after upgrading to version 3.0.0.

    Add following require will solve the issue:

    require 'will_paginate/array'
    

    Check out this post for the backward compatibility of will_paginate 3.0.

  3. The will_paginate documentation states that combining .paginate with .find is not the way to go, because .find will load everything from your DB before .paginate has a chance to restrict the fetch.

Originally posted 2013-11-09 21:39:24.