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
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 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
I have a Model with 2 atrributes:
:image_filename
:yt_video_id
I have this code in my controller:
def index
#search = Model.solr_search do |s|
s.fulltext params[:search]
s.paginate :page => params[:page], :per_page => 2
s.with(:image_filename || :yt_video_id)
end
#model = #search.results
respond_to do |format|
format.html # index.html.erb
end
end
in my model.rb Model I have this in searchable:
searchable do
string :image_filename, :yt_video_id
end
I want filter :image_filename OR :yt_video_id any are not "nil". I mean, both attributes must have a mandatory value.
but I get the error:
Sunspot::UnrecognizedFieldError in ModelsController#index
No field configured for Model with name 'image_filename'
The problem was fixed with the following steps:
(This solution works fine for me. I hope this solution can help you too.)
In model.rb you can not write this syntax:
searchable do
string :image_filename, :yt_video_id
end
You must write this syntax:
searchable do
string :image_filename
string :yt_video_id
end
In your models_controller.rb in index action:
def index
#search = Model.solr_search do |s|
s.fulltext params[:search]
s.paginate :page => params[:page], :per_page => 2
s.any_of do
without(:image_filename, nil)
without(:yt_video_id, nil)
end
end
#model = #search.results
respond_to do |format|
format.html # index.html.erb
end
end
I have used the any_of method.
To combine scopes using OR semantics, use the any_of method to group restrictions into a disjunction:
Sunspot.search(Post) do
any_of do
with(:expired_at).greater_than(Time.now)
with(:expired_at, nil)
end
end
You can see in https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields
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)