I am using ransack for search and now I want to implement pagination in my rails app. So I am using the will_paginate gem. The issue am facing is that I cant figure out how to put the paginate in my current controller code as its already fetching results based on the query.
Here is my controller code
def search
if params[:search].present? && params[:search].strip != ""
session[:loc_search] = params[:search]
end
arrResult = Array.new
if session[:loc_search] && session[:loc_search] != ""
@rooms_address = Room.where(active: true).near(session[:loc_search], 5, order: 'distance')
else
@rooms_address = Room.where(active: true).all
end
@search = @rooms_address.ransack(params[:q])
@rooms = @search.result
@arrRooms = @rooms.to_a
Could someone tell me how to put pagination here?
update with logs
18:29:40 web.1 | Room Load (0.8ms) SELECT rooms.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI() / 180 / 2), 2) + COS(12.9715987 * PI() / 180) * COS(rooms.latitude * PI() / 180) * POWER(SIN((77.5945627 - rooms.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((rooms.longitude - 77.5945627) / 57.2957795), ((rooms.latitude - 12.9715987) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "rooms" WHERE "rooms"."active" = $1 AND (rooms.latitude BETWEEN 12.754501025333727 AND 13.188696374666272 AND rooms.longitude BETWEEN 77.37177993269385 AND 77.81734546730614 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI() / 180 / 2), 2) + COS(12.9715987 * PI() / 180) * COS(rooms.latitude * PI() / 180) * POWER(SIN((77.5945627 - rooms.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 15) ORDER BY distance LIMIT 5 OFFSET 5 [["active", "t"]]
18:29:40 web.1 | (0.3ms) SELECT COUNT(*) FROM "rooms" WHERE "rooms"."active" = $1 AND (rooms.latitude BETWEEN 12.754501025333727 AND 13.188696374666272 AND rooms.longitude BETWEEN 77.37177993269385 AND 77.81734546730614 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI() / 180 / 2), 2) + COS(12.9715987 * PI() / 180) * COS(rooms.latitude * PI() / 180) * POWER(SIN((77.5945627 - rooms.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 15) [["active", "t"]]
Ransack gem is a very powerful and feature-rich gem used widely by Rails community to implement advanced search capability in a Ruby on Rails application. You can create simple as well as advanced search forms for with this Rails search gem.
You paginate the results of your search, so after ransack
.
Something like @rooms = @search.result.paginate(page: params[:page], per_page: params[:per_page])
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With