Forum: Ruby on Rails how do i paginate the results of a query?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7c55f80e769f87db89161af29f175573?d=identicon&s=25 Michael Whitman (Guest)
on 2006-04-12 02:46
(Received via mailing list)
I have a controller and list page for region object.  My list page has a
'name' text input that can be used to limit the list.
It works except that the paginate gets the full set of regions
unqualified
by my search term.  I believe this full list is contained in :regions
symbol
which I dont seem to be allowed to modify or replace (yes I am a ruby
and
rails newb).  How can I paginate the set my region_search method returns
rather than all the regions in my database?  Thanks.

def region_search
    regions = Region.find(:all, :conditions => ["name like ?", "%" <<
params[:name].to_s << "%"])
end

def region_list
    if params[:name].nil? or params[:name].eql?([""])
      logger.info("name is nil or empty")
      @region_pages, @regions= paginate :regions, :per_page => 10
    else
      logger.info("name is not empty")
      #this is kinda weird, seem to need the following line for some
strange
reason
      #I guess the page expects it
      @regions=region_search
      @region_pages, region_search= paginate :regions, :per_page => 10
    end
  end
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-12 03:03
Michael Whitman wrote:
> I have a controller and list page for region object.  My list page has a
> 'name' text input that can be used to limit the list.
> It works except that the paginate gets the full set of regions
> unqualified
> by my search term.  I believe this full list is contained in :regions
> symbol
> which I dont seem to be allowed to modify or replace (yes I am a ruby
> and
> rails newb).  How can I paginate the set my region_search method returns
> rather than all the regions in my database?  Thanks.
>
> def region_search
>     regions = Region.find(:all, :conditions => ["name like ?", "%" <<
> params[:name].to_s << "%"])
> end
>
> def region_list
>     if params[:name].nil? or params[:name].eql?([""])
>       logger.info("name is nil or empty")
>       @region_pages, @regions= paginate :regions, :per_page => 10
>     else
>       logger.info("name is not empty")
>       #this is kinda weird, seem to need the following line for some
> strange
> reason
>       #I guess the page expects it
>       @regions=region_search
>       @region_pages, region_search= paginate :regions, :per_page => 10
>     end
>   end

You can use the :conditions parameter on the paginate method:

@region_pages, @regions = paginate :regions, :per_page => 10,
:conditions => ["name like ?", "%#{params[:name].to_s}%"]

You can't assign region_search the way you've tried here because
region_search is a method, not a variable.  The paginate method returns
two variables, @region_pages and @regions.

Jeff Coleman
This topic is locked and can not be replied to.