Lisa
Lisa

Reputation: 197

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?

Upvotes: 0

Views: 2933

Answers (2)

apneadiving
apneadiving

Reputation: 115511

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 %>

Upvotes: 3

RocketR
RocketR

Reputation: 3766

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

Better move this logic to scopes for this to look prettier and shorter.

Upvotes: 3

Related Questions