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])
Related
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
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
Attempting to sort an entry by an associated field, unfortunately when paginating with Will Paginate the sorting is wrong.
#categories = Category.includes(:posts)
.page(params[:page])
.order("posts.post_at DESC")
I have tried many variations, but unfortunately will paginate does not seem to respect the sort order of categories. Without the pagination the categories are sorted correctly.
How can I get Will Paginate to respect the order?
The end goal is Categories sorted by posts.post_at and paginated while maintaining that order.
Thanks.
Try
#categories = Category.includes(:posts).order("posts.post_at DESC").paginate(:page => params[:page], :per_page => 10)
Sorry can't comment due to low rep, but what are you trying to sort by and what do you want listed?
If you want categories in a certain order with the associated posts, is that by alphabetical?
#categories = Category.includes(:posts).order('categories.name??').page(params[:page])
Category with most recent posts?
#categories = Category.includes(:posts).order("posts.post_at DESC").page(params[:page])
Or are you wanting the most recent posts with the categories listed along with it?
#posts = Post.includes(:category).order('posts.post_at DESC').page(params[:page])
It's unclear what your end goal is with this query.
I am new to Ruby on Rails and am having trouble with a simple where with a model.
When I try to do Test #1 the results are out of order. New items get pushed to the bottom no matter what.
def index
#user = User.where(:status => false).order(last_name: :desc).all
end
If I enter this into rails console it doesn't work also but if I remove the all it works perfectly in rails console but doesn't work in the UsersController.
What is the proper way to do a where clause with an order? Thanks for your help!
UPDATE:
I have updated the code to the following but the results are still out of order:
def index
#user = User.where(status: false).order('last_name DESC')
end
You should use order('last_name DESC') instead of order(last_name: :desc).
order(last_name: :desc) will produce sql like (That's why your order doesn't work):
ORDER BY '---\\n:last_name: :desc\\n'
order('last_name DESC') will produce sql right:
ORDER BY last_name DESC
Rails 4
def index
#user = User.where(status: false).order('last_name DESC')
end
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