I have already implemented a location based search using geocoder and am having trouble integrating the meta_search gem. I'm trying to integrate meta_search into my object_controller index to allow users to filter and sort search results by an objects :attributes after they have already searched by location.
My object_controller:
def index
if params[:search].present?
#objects = Object.near(params[:search], 50, :order => :distance).paginate(:page => params[:page], :per_page => 9)
else
#objects = Object.paginate(:page => params[:page], :per_page => 9)
end
end
Any idea how best to integrate the #search into the index required by the meta_search gem?
Here is what the meta_search github recommends for the index:
def index
#search = Article.search(params[:search])
#articles = #search.all # load all matching records
# #articles = #search.relation # Retrieve the relation, to lazy-load in view
# #articles = #search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
end
Thanks so much,
Will
I believe both the geocoder and meta_search query methods return an ActiveRecord::Relation therefore you should be able to chain them:
#objects = Object.near(params[:search], 50, :order => :distance).search(params[:search]).relation.paginate(:page => params[:page], :per_page => 9)
or if you need the search object to be separate:
#search = Object.near(params[:search], 50, :order => :distance).search(params[:search])
#objects = #search.relation.paginate(:page => params[:page], :per_page => 9)
Related
I'm trying to update the following code on by controller:
#post = Post.paginate_by_user_id(#user.id, :page => params[:page], :per_page => 20, :order => 'created_at DESC')
This was used most likely used with will_paginate plugin for rails 2
paginate_by_user_id or by anything else is no longer used in current will_paginate gem
Does anyone know how, I can pass the #user.id and use Post.paginate and keep the keep same logic as above?
I have everything else need to make this work.
any help would be greatly appreciated.
Thanks,
Try:
#posts = Post.where(:user_id => #user.id).paginate(:page => params[:page], :per_page => 20).order('created_at DESC')
Or if your User model has a has_many relation to posts:
#posts = #user.posts.paginate(:page => params[:page], :per_page => 20).order('created_at DESC')
I've beeen using Ransack in one of my projects and also using Bullet to
spot some N+1 queries in my controllers. However, I'm not quite sure how to accomplish that while using Ransack.
There are two models involved, Patch and Image. And a Patch has_one Image.
The action code is the follow:
def index
#q = Patch.search(params[:q])
#patches = #q.result(distinct: true).order("code DESC").paginate(:page => params[:page], :per_page => 10)
end
Any thoughts?
This is working to me in a project.
def index
#q = Client.includes(zone: :user).ransack(params[:q])
#clients = #q.result.page(params[:page]).decorate
end
In your case should be
def index
#q = Patch.includes(:image).search(params[:q])
#patches = #q.result(distinct: true).order("code DESC").paginate(:page => params[:page], :per_page => 10)
end
I can't seem to find the correct way to set a maximum number of messages to display in a users inbox without it disfiguring the pagination. I'm trying to make it so only the last 100 inbox messages are displayed from newest to oldest.
messages_controller.rb
class MessagesController < ApplicationController
def index
#messages = current_user.received_messages.paginate(:page => params[:page], :per_page => 15, :order => 'created_at DESC', )
end
using the will_paginate gem
<%= will_paginate #messages %>
def index
#messages = current_user.received_messages.paginate(:page => params[:page], :per_page => 15).order('created_at DESC').limit(100)
end
or try with
def index
#records = current_user.received_messages.order('created_at DESC').limit(100)
#messages = #records.paginate(:page => params[:page], :per_page => 15)
end
Hope this will work
But it is a good practice to first implement the active_records conditions, than the pagination's.
def index
#messages = current_user.received_messages.order(:created_at).reverse_order.limit(100).paginate(:page => params[:page], :per_page => 15)
end
Hope it will help.
I have this problem before, My fix was:
require 'will_paginate/array' # To paginate an array instead of ActiveRecord
class MessagesController < ApplicationController
def index
#messages = current_user.received_messages.limit(100).all.paginate(:page => params[:page], :per_page => 15, :order => 'created_at DESC') # I transform the resultset to an array using .all before the paginate
end
In my controller for the index action, I have;
def index
#documents = current_user.documents.all if current_user
end
Anything I add on the end of current_user I get an error. For example, I tried to add will_paginate which is simply adding .paginate(:per_page => 5, :page => params[:page]) to the end of the index action and adding <%= will_paginate #documents %> to the views.
Once I add the .paginate(:per_page => 5, :page => params[:page]) to the end of the index method, like this;
def index
#documents = current_user.documents.all if current_user.paginate(:per_page => 5, :page => params[:page])
end
I get a NoMethodError. Anybody know how to fix this?
Try
def index
#documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user
end
You're calling the method on the wrong object, here is the correct one:
#documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user
In my project model
def incomplete
#clients = current_user.clients.find_all_by_completed(false).paginate
(:page => params[:page], :per_page => 10, :order => 'started_on DESC')
end
For some reason it doesn't order started_on descending. However ordering works in another method
def all
#clients = current_user.clients.paginate(:page => params[:page], :per_page => 25, :order => 'started_on DESC')
end
So I'm assuming using find_all_by_completed is throwing off paginate. I'm using will-paginate btw. Any help?
Try passing the condition explicitly:
#clients = current_user.clients.paginate(
:conditions => {:completed => false},
:page => params[:page], :per_page => 10,
:order => 'started_on DESC')