How can I order when using pagination? - ruby-on-rails

This might be quite simple question.
Here's my code. Then I want to sort #codes ordered by the column code.user.last_active_at
#here, I retrieve only the records that contains particular keyword.
#search = Code.search do
fulltext params[:search]
with(:community_id, community_search)
paginate :page => 1, :per_page => 1000000
end
#Here, I try pagination. 10 records per each page.
#codes = #search.results
#codes = Kaminari.paginate_array(#codes).page(params[:page]).per(10)
I want to sort them ordered by the column #code.user.last_active_at
How can I do that?
I tried this
#codes = Kaminari.paginate_array(#codes.joins(:user).order('user.last_active_at DESC')).page(params[:page]).per(10)
But it returned this error
NoMethodError (undefined method `joins' for #
<Sunspot::Search::PaginatedCollection:0x0000001ca851a8>):
app/controllers/codes_controller.rb:95:in `index'
How can I sort while I'm using Kaminari pagination?

Kaminari not affect on sorting. first you have to sort and then get page(x).per(x)
so for example in your controller
#codes = #search.results
#codes = #codes.joins(:user).order('user.last_active_at DESC').page(params[:page]).per(10)
edit:
after check sunspot documentation I think you can make something like
#search = Code.search(include: :user) do
fulltext params[:search]
with(:community_id, community_search)
order_by('user.last_active_at DESC')
#paginate :page => params[:page], :per_page => 10 you can add this and not use Kaminari
end
you can try joins except include

Related

Rails - Combining will_paginate and ransack

I am using Ransack for Filtering and will_paginate for Pagination and it seems I have a problem combining both.
Analyzing memory problems of my app I saw that my Category controller was loading all entries from the database at each call.
def index
#q = Category.all_cached.paginate(:page => params[:page], :per_page => 30).ransack(params[:q])
#categories = #q.result(distinct: true)
end
In order to get the only the first entries for each page I tried the following change:
def index
#q = Category.paginate(:page => params[:page], :per_page => 30).ransack(params[:q])
#categories = #q.result(distinct: true)
end
Problem is that now the pagination is not working any longer.
Error says: Couldn't find Category with 'id'=x even though enough records are available.
I have two questions:
How can I avoid loading all entries at first call but instead per
page
As I also retrieve information about different products in
the category and I want to avoid n+1 calls, how can I add this
[includes(:product)] into the Category Controller?
Try this way:
def index
#q = Category.paginate(page: params[:page], per_page: 30).ransack(params[:q])
#categories = #q.result(distinct: true).includes(:products)
end

Pagination with Rails 5 - Having two models on "newsfeed" and paginating the total?

I am building a web app that has a "newsfeed" with two models (called Event and Suggestion). Is it possible to paginate the newsfeed such that the pagination applies to the total and not either individually? Here is what I have so far in my controller. I tried simply adding the two, though that doesn't work because the result is an array, not an ActiveRecord Association. Thanks in advance!
def index
#ideas = Suggestion.paginate(:page => params[:page], per_page: 25).order('created_at DESC')
#ideas += Event.paginate(:page => params[:page], per_page: 25).order('created_at DESC')
render :index
end
Assuming you use 'will_paginate', try this. Put this in your initializers:
require 'will_paginate/array'
then you can do things like this:
#objects = (Suggestion.all + Event.all).sort_by &:created_at
#objects.paginate(:page =>params[:page], per_page: 25)
The problem here is performance. This way you will get all records from the database every time, and then perform pagination

Group Solr results by class

I'm searching two models with Solr like so:
#query = Sunspot.search Location, Employee do
with(:category_id, params[:category_id]) if params[:category_id].present?
fulltext params[:search]
facet :category_id
order_by(:weight, :desc)
paginate :page => params[:page], :per_page => 10
end
I would like to create two discrete objects from the results, containing matches from both models so I can split them up in the UI.
I'm currently doing:
#results = #query.results.group_by(&:class)
#locations = #results[Location]
#employees = #results[Employee]
but that's not working. #locations and #employees are both empty. What am I missing here? If i debug #results, I see matches and they're already grouped by model. I just want to expose that to my view so I can split up results into hideable/showable containers. Thoughts?
I don't have solr, so I can't assume exactly what #results looks like (I assume it's a hash).
With that assumption, the hash keys are either Symbols or Strings.
So try
#locations = #results[:Location]
#employees = #results[:Employee]
or
#locations = #results["Location"]
#employees = #results["Employee"]
One of the two of those (probably the first) should give you the information in each instance variable. The reason is that ruby will set the hash keys of class names as symbols. If the names were already strings, the keys will be strings.

Error with Kaminari pagination in Rails 3

I'm trying to implement pagination in my Rails 3 app with Kaminari but I'm getting the following error:
undefined method `page' for #<Array:0x007fe43f4b0e80>
This is what I have in my controller:
#stories = Story.find_all_by_keynote_id(#keynote, :order => 'created_at DESC').page(params[:page])
And this is what I put in my view:
<%= paginate #stories %>
I think there's a problem with the "find_all_by_keynote_id" but I'm not sure how to fix it.
Thank you!
Kaminari can also paginate arrays (which is what you have)
array = Story.find_all_by_keynote_id(#keynote, :order => 'created_at DESC')
#stories = Kaminari.paginate_array(array).page(params[:page])
Yes, but how to query the same without an array? That's what I need.
#stories = Story.where(:keynote_id => #keynote).order('created_at DESC').page(params[:page])
or
Keynote.has_many :stories
#stories = #keynote.stories.order('created_at DESC').page(params[:page])
You are feeding the page method an array when it is expecting an Active Record Relation. If you aren't familiar with that term then you should have a look at the active record query guide.
According to the Kaminari docs this is how you are supposed to use it:
#stories = Story.order(:created_at).page(params[:page])

Rails Will Paginate problem

I´m paginating with the will_paginate plugin.
I have a popup that adds #products when the user clicks the button Add via an Ajax request.
Note: The #products is paginated
In Ajax Request I update the product-list :
ids = params[:accessories][:id].join(" and id <> ")
#products = Product.find_by_sql("select * from products where id <> #{ids}")
However if I use: #products = Product.find_by_sql("select * from products where id <> #{ids}").paginate
it returns identical array.
So how I could paginate the array again?
Why are you using find_by_sql method?
May be you can write something like this:
ids = params[:accessories][:id].join(",")
#products = Product.paginate(:page => params[:page], :conditions => ["not (id in (?))", ids])
also conditions you can move to product model and replace it with scope(Rails 3) or named_scope(Rails 2).
Already tried:
#products = Product.paginate :conditions => conditions

Resources