How do i paginate the results of a query?


#1

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


#2

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 C.man