Paginate Multiple Models in Kaminari

Paginate Multiple Models in Kaminari

Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:

@results = @users + @posts + @comments
@results.sort! { |a, b| a.score(query) > b.score(query) }

Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.

Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:

User.multi_solr_search query, models: [Post, Comment]

Rails: How do I paginate multiple models with kaminari?

Kaminari handles arrays...

Kaminari.paginate_array(@activities).page(params[:page]).per(10)

Paginating Multiple Objects of the Same Model on the Same Page with Kaminari

The reason why this problem is happening is because you are using the same params[:page] attribute to navigate through the pages. This results in the same number being passed to both the queries for your Product model. You will need to do this instead to get it working.

Partial for registered page:

...
<%= paginate @registered_products, param_name: :registered_page_no %>

And in the unregistered page partial, you will have to do the same, but with a unique name like unregistered_page_no

In your controller, all you need to do is this:

class HomeController < ApplicationController
def index
@registered_products = Product.where(product_type: 0).page params[:registered_page_no]
@unregistered_products = Product.where(product_type: 1).page params[:unregistered_page_no]
end
end

To know more about param_name, read the docs: https://github.com/kaminari/kaminari#changing-the-parameter-name-param_name-for-the-links

Multiple pagination with kaminari via Ajax

you are missing to pass params at this line

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');

i think it should be like this

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true, :param_name => 'page_2').to_s) %>');

and you are also passing wrong param at this line

<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>

it should be like this

<%= paginate @bookmarks,:remote => true, :param_name => 'page_2' %>

and please also check that whether you are sending the response correctly to the JS file or not.

Paginate child models Kaminari on rails 3.1

@categories.each do |category| 
@images = Kaminari.paginate_array(category.images).page(1).per(1)
end

In this loop, you're overriding @images each time, so obviously, you wont get what you expect ;)

I think that what you wanna do is:

@images = Image.where("category_id IS NOT NULL").page(params[:page]).per(20)

The .where("category_id IS NOT NULL") is not necessary if images have to belong to a category.

Paginating a model with attributes from multiple Arrays

The answer to the question lies in using WillPaginate::Collection to create your own pagination structure.

Here is the rubydoc link: http://rubydoc.info/github/mislav/will_paginate/frames

Paginate by most recent post with kaminari

in your model you can do:

default_scope order("created_at DESC")  

or

default_scope order("created_at ASC")  


Related Topics



Leave a reply



Submit