简体   繁体   中英

rails pagination for custom queries

I have the following custom query:

@initial_matches = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ? AND years <= ? AND degree_type <= ?", current_user.industry, current_user.years, @highest_degree])

Any way to paginate the results?

Listing.where(:industry => current_user.industry).where(["years <= ?", current_user.years]).where(["degree_type <= ?", @highest_degree]).paginate

最好将此逻辑移到范围内,以使其看起来更漂亮或更短。

Will_paginate works fine with basic Arrays.

In your controller:

@stuffs = %w(1 2 3 4 5 6 7 8 9 10).paginate(:page => params[:page], :per_page => 4)

In your view:

<% @stuffs.each do |stuff| %>
 <%= stuff %>
<% end  %>
<%= will_paginate @stuffs %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM