Pagination on multiple searches - ruby-on-rails

I have the following bit of code:
def index
#games = Game.gamsearch(params[:gamsearch])
#games = Game.consearch(params[:consearch]) if params[:consearch].present?
#games = Game.gensearch(params[:gensearch]) if params[:gensearch].present?
#games = Game.where("game_name LIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
#games = Game.where("console = ?", params[:console]) if params[:console].present?
end
What I wish to do is have all of these results paginate. Now I know how to paginate from a singular search as I have done here:
def index
user_relation = case params[:username].present?
when true then User.where("username LIKE ?", "#{params[:username]}%")
else User
end
#users = user_relation.paginate :page=>params[:page], :order => 'created_at desc', :per_page => 10
end
But the games controller has 5 possible methods of search and was wondering how I would apply pagination to it.
Any help would be great, thanks.

This should be possible by appending the search results all to the same array and than paginate this array.
like:
def index
#games = Array.new()
#games << Game.gamsearch(params[:gamsearch])
#games << Game.consearch(params[:consearch]) if params[:consearch].present?
#games << Game.gensearch(params[:gensearch]) if params[:gensearch].present?
#games << Game.where("game_name LIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
#games << Game.where("console = ?", params[:console]) if params[:console].present?
end
and then just paginate the #games object.
hope this helps!

Related

How to convert SQL syntax into Mongo?

I use Ruby on Rails and Mongoid
How to convert this syntax to make it work with Mongo?
def index
#conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)
end
and
def index
#conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)
end
found a solution:
#conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)
change to
#conversations = Conversation.any_of({sender_id: current_user.id}, {receiver_id: current_user.id})
The second line had to be completely rewritten and not used. So I don't know if it works or not
#conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)
may not work
#conversation.messages.not.where(user_id: current_user.id).where(read: false).update_all(read: true)

will_paginate giving an error when doing a search

Alrighty, so I have searched on here repeatedly on this as well as other places but have yet to find the solution that relates to me. Probably because the previous questions were some time ago.
My problem is that I had first added pagination and then I was required to add a search so that users could search for products. When they do the search it's just supposed to open the products page. If I took out the search, the pagination doesn't give me an error.
The error I get now is
''undefined method `total_pages' for # Product::ActiveRecord_Relation:''
and the line of code highlighted for the error is the pagination in the index.html.erb.
What am I missing? Thanks for any guidance, this newbie needs it!
This is the products_controller:
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
if params[:q]
search_term = params[:q]
#products = Product.search(search_term)
else
#products = Product.all
#products = Product.paginate(:page => params[:page], per_page: 4)
end
end
This is the index.html.erb :
<div class="pagination">
<%= will_paginate #products %>
</div>
You have missed paginate method when the search, it #products = Product.search(search_term) will be like this
.....
if params[:q]
search_term = params[:q]
#products = Product.search(search_term).paginate(:page => params[:page], per_page: 4)
else
#products = Product.all.paginate(:page => params[:page], per_page: 4)
.....
Additionally Remove this #products = Product.all don't need this.
After all, you just paste this instead of your code, it reduced
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
if params[:q]
search_term = params[:q]
#products = Product.search(search_term)
else
#products = Product.all
end
#products = #products.paginate(:page => params[:page], per_page: 4)
end
Modify index action as follows:
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
#products = params[:q] ? Product.search(params[:q]) : Product.scoped
#products.paginate(:page => params[:page], per_page: 4)
end
you should use paginate for searching too.

Searchkick get all results when using pagination

I have the following search query for products using searchkick:
def index
#filter = params[:filter].blank? ? nil : Search.find(params[:filter])
if #filter.present?
#products = #filter.products(set_order_column(#sort), #direction, params[:page], #per_page)
else
query = #query.presence || "*"
#products = Product.search(
query,
where: {
active: true
},
page: params[:page],
per_page: #per_page,
order: [{
"#{set_order_column(#sort)}" => "#{#direction}"
}],
misspellings: { distance: 2 },
fields: fields)
end
#filter_product_ids = #products.map(&:id)
end
There is the variable filter_product_ids where I need to store all results not filtered results by #per_page. Is it possible to do that? Right now, there are results just for results #per_page.
The goal to get all the results without pagination is to get uniq values for all products used to various product categorization for filtering on the website.
Thank you for any hints, Miroslav
My solution is to put this in it's own class (service class), then
class ProductFilter
attr_reader :search_params, :search_query, :pagination_params
def initialize(search_params, search_query, pagination_params)
#search_params = search_params
#search_query = search_query
#pagination_params = pagination_params
end
# Will return all records from searchkick, unpaginated
def filtered_products
Product.search(search_query, search_param_stuff)
end
# Will return all filtered_products & then paginate on that object
def paginated_products
filtered_products.records.paginate(pagination_params)
end
private
def search_param_stuff
search_params
end
end
In any case, searchkick's method for pagination actually works on the searchkick records object, so you can just pull the ES query (without passing in pagination params) in one method, then paginate on the filtered_products.records.
Just remove
params[:page], #per_page
and
page: params[:page],
per_page: #per_page,
from your code.

How can i delete from list while using ransack search in rails

i am trying to delete from list but when i am trying this it is getting deleted from database
#course = Course.find(params[:id])
#search = Lesson.search(params[:q])
#lessons = #search.result.paginate(:page => params[:page], :per_page => 10)
#search.build_condition if #search.conditions.empty?
#course.lessons.each do |lesson|
#lessons.each do |l|
if lesson.id == l.id
#lessons.delete(l)
end
end
end
I am getting this error: delete_all doesn't support limit scope
Thanking you
Delete is an ActiveRecord method. I assume you don't want to delete it from the database but from the result list. You can do it like this:
#course.lessons.each do |lesson|
#lesson.reject { |l| l.id == lesson.id }
end

Order array. undefined method `order' for Array. Convert array into hash?

I have a City model and in city's show action I want to render hotels nearby specific locations in the city. Cities has_many locations; hotels are being searched using Geocoder near method.
To add order functionality I've followed Ryan Bates screencasts #228, but this approach doesn't seem to work with arrays, giving error undefined method `order' for #< Array:0x007f960d003430>
cities_controller.rb
helper_method :sort_column, :sort_direction
def show
session[:search_radius] = 2 if session[:search_radius].blank?
#city = City.find(params[:id])
#locations = #city.locations
#hotels = []
#locations.each do |location|
unless location.longitude.blank? || location.latitude.blank?
center_point = [location.latitude, location.longitude]
box = Geocoder::Calculations.bounding_box(center_point, session[:search_radius])
thotels = Hotel.near(center_point, session[:search_radius]).within_bounding_box(box)
else
thotels = Hotel.near(center_point, session[:search_radius])
end
#hotels += thotels if thotels
#hotels = #hotels.uniq
end
#hotels = #hotels.order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 5)
#json = #locations.to_gmaps4rails
respond_with #json, :location => city_url
end
private
def sort_column
Hotel.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
My question is: should I concentrate in converting an array into hash or should I initially create hash of hotels, or maybe find completely different approach to perform sorting?
order is a method used for sorting at the database level. since #hotels is an array, you won't be able to sort using order. Try the following (not tested and you may want to include array pagination if you haven't included it yet)
#hotels = #hotels.sort_by(&:"#{sort_column}")
#hotels = #hotels.reverse if sort_direction == 'DESC'
#hotels = #hotels.paginate(:page => params[:page], :per_page => 5)

Resources