{"id":689,"date":"2022-08-30T15:05:32","date_gmt":"2022-08-30T15:05:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-cant-i-do-this-in-my-controller-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:32","modified_gmt":"2022-08-30T15:05:32","slug":"why-cant-i-do-this-in-my-controller-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-cant-i-do-this-in-my-controller-collection-of-common-programming-errors\/","title":{"rendered":"Why cant I do this in my controller-Collection of common programming errors"},"content":{"rendered":"<pre><code>def search\n  @location = Location.find(params[:location_id])\n  start_date = DateTime.strptime(params[:start_date], \"%m-%d-%Y\")\n  end_date = DateTime.strptime(params[:end_date], \"%m-%d-%Y\")\n  @songs = @location.songs.find(:all, :conditions =&gt; {:play_date =&gt; start_date..end_date}).paginate(:page =&gt; params[:page], :per_page =&gt; 40)\n  render 'show'\nend\n<\/code><\/pre>\n<p>Here is my error<\/p>\n<pre><code>undefined method `paginate' for #\n<\/code><\/pre>\n<p>all works if i remove the will_paginate but i need it&#8230;any ideas or is there a better way to write this controller<\/p>\n<ol>\n<li>\n<p>Try writing<\/p>\n<pre><code>@songs = @location.songs.where(:play_date =&gt; start_date..end_date).paginate(:page =&gt; params[:page], :per_page =&gt; 40)\n<\/code><\/pre>\n<p>The difference? <code>where<\/code> returns an <code>ActiveRelation<\/code> object, while <code>find<\/code> retrieves all the matching objects in an array.<\/p>\n<p>Hope this helps.<\/p>\n<\/li>\n<li>\n<pre><code>NoMethodError: undefined method `paginate' for []:Array\n<\/code><\/pre>\n<p>My will_paninate works perfectly but above error jumped out after upgrading to version 3.0.0.<\/p>\n<p>Add following require will solve the issue:<\/p>\n<pre><code>require 'will_paginate\/array'\n<\/code><\/pre>\n<p>Check out this post for the backward compatibility of will_paginate 3.0.<\/p>\n<\/li>\n<li>\n<p>The will_paginate documentation states that combining <code>.paginate<\/code> with <code>.find<\/code> is not the way to go, because <code>.find<\/code> will load everything from your DB before <code>.paginate<\/code> has a chance to restrict the fetch.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:39:24. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>def search @location = Location.find(params[:location_id]) start_date = DateTime.strptime(params[:start_date], &#8220;%m-%d-%Y&#8221;) end_date = DateTime.strptime(params[:end_date], &#8220;%m-%d-%Y&#8221;) @songs = @location.songs.find(:all, :conditions =&gt; {:play_date =&gt; start_date..end_date}).paginate(:page =&gt; params[:page], :per_page =&gt; 40) render &#8216;show&#8217; end Here is my error undefined method `paginate&#8217; for # all works if i remove the will_paginate but i need it&#8230;any ideas or is there a better [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}