The number of items for a page in Kaminari is defined by:
defining per_page member in model, or
defining default_per_page in config.
My question is, how to obtain that number of items in view?
Update
Pardon me for being not clear. What I want is to show is the order of item in each page. I have a table header:
<th>#</th>
And several columns below:
<td><%= (#sales.current_page - 1) * some_ruby_code + index + 1 %></td>
My question is what some_ruby_code should be?
1) If I substitute some_ruby_code with Sale.per_page, it will throw error when I decided to delete per_page.
2) If I substitute some_ruby_code with Kaminari.config.default_per_page, it won't show the proper value when I add per_page in model.
I want to know whether there are a method in Kaminari which detects the existence of per_page, return its value if exists, or Kaminari.config.default_per_page otherwise.
Thank you!
You can use Sale.default_per_page.
See https://github.com/kaminari/kaminari/blob/1-0-stable/kaminari-core/lib/kaminari/models/configuration_methods.rb#L16
You can use ActiveRecord's limit_value method for this, since Kaminari internally uses limit and offset.
User.all.limit(10).limit_value
=> 10
With kaminari is the same:
User.page(3).per(10).limit_value
=> 10
User.page(3).per(10).offset_value
=> 20
I guess, you want to display the number of items visible out of the total,
if so you can use
<%= page_entries_info #items %>
Read this documentation on github:
https://github.com/amatsuda/kaminari
Assuming you actually have the items for that page being rendered in the view, this should work:
#items.length
current_per_page returns of given list of paged records.
Related
COUNT query to find out total_count sometimes very slow.
But if I am caching values of items_amount or such field in model I want to use it for kaminari pagination.
EXAMPLE: I have model
Category (:id, :title, :items_amount)
And then on show view I want to
<%= paginate #items, total_count: #category.items_amount %>
So like paginate_array, but in paginate. It is very useful for perfomance.
Is there a feature like this or how I can add it?
Like will_paginate gem, where we can do it like this
.paginate(page: params[:page], per_page: 72, total_entries: #category.items_amount)
Kaminari just calls the total_count method on #items in order to get the count. By default this does the SELECT count(*) ... query but you could decorate #items to be something that provides your cached count.
If you are just generating page links though then paginate will take a total_pages option. By providing the page count it means that the paginate call doesn't need to get the total count. (I can't find that option explicitly documented but it is in the code and I've tested using it).
paginate #items, total_pages: #cached_page_count
#cache_page_count might have a value like (#category.item_amount / 30.0).ceil in order to get the correct page count with 30.0 being your page size.
I think they answer this really well in their gems readme? https://github.com/kaminari/kaminari
You can do this on controller level using paginates_per to set the limit on each page and max_paginates_per to set the limit of pages?
I have been searching and searching for help on this question, but I have found no luck. I am wanting to have a page that displays each user in my database one at a time, and when I press a 'Next' button, it will go to the next user in the database and display their information. I have been seeing this code everywhere:
<% #users.each do |user| %>
<li>
# Add some code
</li>
<% end %>
but this is displaying every user all at once on my page. I would like to only display a single user at a time. If anyone could direct me to a good source or has a simple explanation, I would be forever grateful!
You just need to paginate the #users records. I prefer using will_paginate gem.
After including it in your gem file, you may use it like:
#users = User.paginate(:page => 1, :per_page => 1)
For basic understanding, the gem basically uses limit and offset in SQL query to attain the record. If you don't want to use the gem, then you may try like:
User.limit(1).offset(params[:offset].present? ? params[:offset] : 0)
And on clicking on next you may pass the incremented value of offset each time as parameter.
have a look at the kaminari gem. You will be able to paginate your results one by one (one result per page, hence the name).
I would like to have a link that only displays the last 10 database submissions with a select number of tables per submission. Hope that makes sense. So in short a can click 'recent submissions' and it will display the last 10 via customer name, submission date, and id number . Currently in the controller i have the following just to test the link but of course it display every table.
<h2>LAST 10 SUBMISSIONS</h2>
<%= #posts.each do |post| %>
<%end%>
Sorry, this is more of a comment than an answer but I don't have enough rep to leave a comment.
You could retrieve a bunch of #posts, which will be an array of ActiveRecord Relations. That means you can use Ruby's nifty Array methods. Here's an example:
#sorted_posts = #posts.sort_by { |p| p.created_at }.take(20)
And then pass in #sorted_posts where you currently have #posts in the code you posted above. But for performance and good practice you should probably retrieve that data in the proper format from the query itself.
#last_twenty_posts = Post.order("created_at DESC").limit(20) # limit by whatever number you want
Hope that helps!
You can simply order the posts by some attribute specifying the submission time (e.g., created_on) and limit the results to a number you want. For example:
Post.order("created_on DESC").limit(10)
This would return the last 10 Post objects based on their created_on attribute, with the newest first.
I would like to display whatever number of items has been found and returned to the user. In my database, there are 4 items. The search feature works fine. What I now want to display is whatever number of records has been found. If user searches "aI", 2 items gets returned and I want to display the text that 2 items has been found. I tried to do that in view --> layout --> application.html.erb.
You have to count the actual results. Product (with a capital p) will always return all objects of that class in the database. Hence 26. Save the result in an ivar (#products) and call count on that in your view instead.
In Controller
#products = Product.fuzzy_search2(params[:search_string])
In View
<% if #products %>
<span> <%= #products.count %> Books Found</span>
<% end %>
I'm assuming that you are storing the search results in an ivar. You need to call .count on that ivar or .size / .length if it's stored in an array. The issue is that when you are calling Product.count you are getting the count of all products in your database always, because that's exactly what you are telling it to do.
This is probably pretty basic, but I'm trying to figure out how to show how many comments a post has in rails, on the post index page of my app.
comments belongs_to post, and post has_many comments
Just not sure how to show on the index page, the amount of comments for each post.
Thanks in advance!
Elliot
Try this.
<%= post.comments.size %>
You might also be interested in the pluralize method.
<%= pluralize(post.comments.size, 'comment') %>
This will output "3 comments" or "1 comment", etc.
I may be wrong here, but you should use
<%= post.comments.count %> rather than size.
ActiveRecord knows that 'count' is a special method, and will turn it into a SELECT count(id) from comments where post_id = x (which is what you want).
size however, is not a special method, and ActiveRecord will load all the comments into an array in memory (SELECT * from comments where post_id = x, and then tell you how long this array is, which may be unneccessary - if you're going to loop through the array of comments further down the page, then you may want to use size to load them into memory, because it will need to happen later anyway.
You should also use some of ActiveRecord's built in functionality here. The counter_cache. Check it out here