Topic: Pagination of find_by_? results

I want to paginate some simple search result. Is the approach I have tried below possible? I think I may be going about this the wrong way.

  def list
    @person_pages, @persons = paginate :persons, :per_page => 10, :order =>"id"
  end
 
  def search
    @persons = Person.find_by_surname(params[:query])
    @person_pages, @persons = paginate :persons, :per_page => 10, :order =>"id"
    render_action 'list'
  end

Re: Pagination of find_by_? results

The paginate line in the search method resets the @persons variable, so it will find all people. You need to set the condition inside the paginate method like this:

  def search
    @person_pages, @persons = paginate :persons, :per_page => 10, :order =>"id", :conditions => ['surname = ?', params[:query]]
    render_action 'list'
  end

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

Thanks Ryan.

Re: Pagination of find_by_? results

I also had such a need. But when I am doing as Ryan suggested, the first page results are good but when I click on next page
I get the following error

NoMethodError in Am applicationsController#results

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.+

RAILS_ROOT: C:/InstantRail/rails_apps/portfolio/config/..
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/am_applications_controller.rb:28:in `results'
-e:3:in `load'
-e:3

C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235
C:\InstantRail\ruby\bin\mongrel_rails:18:in `load'
C:\InstantRail\ruby\bin\mongrel_rails:18

C:\InstantRail\ruby\bin\mongrel_rails:18
-e:3:in `load'
-e:3

Request

Parameters: {"page"=>"2"}

Show session dump

---
:id: 10001
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

Re: Pagination of find_by_? results

The query parameter probably isn't getting passed onto the next page. You need to specify any search parameters in the links.

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

Hi ryanb,
I am sorry, I could not understand how to resolve it.


To better understand what I am doing here I am posting my View and Controller code

Controller code

 def results
       @title = 'List of Applications matching the result '
       @am_application_pages, @am_applications = paginate :am_applications,  :conditions => ['business_application_name LIKE ?',  params[:app_name] + '%'], :per_page => 10
      render :action => 'list'
   end

View code


# list.rhtml
<table>
<tr>
<th> <%= @title %>
</tr>
<tr>
<th>Application Name|</th>
<th>Maintained By|</th>
<th>Primary Contact|</th>

</tr>
<% for am_application in @am_applications %>
<tr>
<td><%=h am_application["business_application_name"] %></td>
<td><%= am_application["maintained_by"] %></td>
<td><%= am_application["primary_contact"] %></td>
<!--td><%= am_application["updated_on"].strftime("%I:%M %p %d-%b-%y") %></td-->

<td><%= link_to 'Show', :action => 'show', :id => am_application %></td>
<td><%= link_to 'Edit', :action => 'edit', :id => am_application %></td>
<td><%= link_to 'Delete', {:action => 'destroy', :id => am_application},
:confirm => "Are you sure you want to delete this category?", :post => true %></td>

</tr>
<% end %>
</table>
<br />
<%= link_to 'Enter New Application', :action => 'new' %>
&nbsp;&nbsp;
<%= link_to 'Search', :action => 'search' %>

<%= link_to 'Previous page', { :page => @am_application_pages.current.previous } if @am_application_pages.current.previous %>
<%= link_to 'Next page', { :page => @am_application_pages.current.next } if @am_application_pages.current.next %>


Where do you think I need the change?
Thanks for your time.

Re: Pagination of find_by_? results

The error is being generated here:

['business_application_name LIKE ?',  params[:app_name] + '%']

The error occurs when the params[:app_name] is nil because it doesn't know how to add the percent sign to nil. To solve this you need to pass the app_name parameter along with the pagination links. Like this:

link_to 'Previous page', { :page => @am_application_pages.current.previous, :app_name => params[:app_name] }

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

Thanks Ryan,

I am going to try that

Re: Pagination of find_by_? results

Thanks Ryan,

That worked. One final question
How would it work if I want to display the page numbers on the bottom of the search results

I currently have this logic

<% if @am_application_pages.page_count>1 %>
<hr />
Page: <%=pagination_links @am_application_pages%>
<hr />
<% end %>

Also could you please suggest me what document/book would I refer to if I myself need to find the answers?
Again as always appreciate the service you provide to us beginners.

Re: Pagination of find_by_? results

railrocks06 wrote:

That worked. One final question
How would it work if I want to display the page numbers on the bottom of the search results

I currently have this logic

<% if @am_application_pages.page_count>1 %>
<hr />
Page: <%=pagination_links @am_application_pages%>
<hr />
<% end %>

Is that not working?



railrocks06 wrote:

Also could you please suggest me what document/book would I refer to if I myself need to find the answers?
Again as always appreciate the service you provide to us beginners.

I normally just use the API, but that takes some getting used to. The Agile Web Development with Rails book helps too. There's also the Rails Wiki. Beyond that, I just use Google to find the answers.

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

ryanb wrote:
railrocks06 wrote:

That worked. One final question
How would it work if I want to display the page numbers on the bottom of the search results

I currently have this logic

<% if @am_application_pages.page_count>1 %>
<hr />
Page: <%=pagination_links @am_application_pages%>
<hr />
<% end %>

Is that not working?

The above code is working. I was just wondering what would be the syntax to send the  params back to controller in the above format.

Also about the paginate
It only works

 @am_application_pages, @am_applications = paginate :am_applications,  :conditions => ['business_application_name LIKE ?',  params[:app_name] + '%'], :per_page => 10

but not

 @am_search = AmApplication.find(:all, :conditions => ['business_application_name LIKE ?',  params[:app_name] + '%'])
@am_application_pages, @am_applications = paginate @am_search, :per_page => 10

what gives?
I know @am_search is an instance variable in ruby but what does : denote.

Re: Pagination of find_by_? results

The colon is for a symbol. Although there are some differences, it helps to just think of it as a string. The way pagination works is a little weird. I suggest just sticking with the examples instead of splitting the find onto a second line.

Regarding your first question, you can append params like this:

<%= pagination_links @am_application_pages, :params=> { :app_name => params[:app_name] } %>

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

Hi ryan,

The problem is I am not sure how to paginate the results below.

 @am_applications = AmApplication.find :all, :include => :am_servers, :conditions => 'server_name like ?', params[:server_name]+ '%'], :order => 'business_application_name'

Re: Pagination of find_by_? results

Does this work?

@am_application_pages, @am_applications = paginate :am_applications,  :include => :am_servers, :conditions => 'server_name like ?', params[:server_name]+ '%'], :order => 'business_application_name', :per_page => 10

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

I tried that earlier and that did not work.

i get the following error
ActiveRecord::StatementInvalid in ReportingController#applications_results

OCIError: ORA-00904: "SERVER_NAME": invalid identifier: select * from (select raw_sql_.*, rownum raw_rnum_ from (SELECT id FROM am_applications WHERE (server_name like 'u%') ORDER BY business_application_name ) raw_sql_ where rownum <= 10) where raw_rnum_ > 0

RAILS_ROOT: C:/InstantRail/rails_apps/portfolio/config/..



As you can see the SQL statement only recogonizes the am_applications it does not recogonize the am_server table

Re: Pagination of find_by_? results

Hmm, I guess the paginator doesn't like the :include parameter. I guess you'll have to do this find separately. The Rails Wiki shows how to do this - check out the Custom Pagination section about halfway down. Also see this tutorial on an alternative way to do it.

If neither of those work for you, I can try to make something.

Railscasts - Free Ruby on Rails Screencasts

Re: Pagination of find_by_? results

The rails Wiki solution worked for me.
Thank you .