rails order by field and get 10 items from the middle - ruby-on-rails-3

I have build a page that displays a list of ratings in an desc order.
I want to display only 10 items at a time. how do I select only, say the 11-20 rows
from the ordered array.
Is it possible to handle this in the query itself, or do i have to fetch the whole table and screen the relevant items?
#pics = Rating.order('rating desc').limit(10)

In this specific case, use .offset(10) in your current query.
In general, search for "rails pagination" on google or here on SO.

Related

keyset pagination for specific to jump page

is keyset pagination in front end only for next and previous ? because what I have learn about it, I could just use that N keep it for previous and. next
let say this query for page one,
SELECT * FROM nameTable ORDER BY ASC id LIMIT 10
and we save the last id on N
and then for next SELECT * FROM nameTable WHERE id > N ORDER BY ASC id LIMIT 10
and if for previous just use WHERE id < N ?
how about if in Client want to jump to the page 10 or back to 3 pages ??
can u all tell me how to do that and is that possible using keyset ?
Using keyset pagination you can not jump to a given page.
You can just go to first, last previous and next.
As explained by Laurenz, you can still move/skip a number "pages" from your current stand but I am not really sure what would be the use case for it.
The main objective of keyset pagination is to avoid the use of the offset/skip - limit for large sets of data, but if you want to jump to an exact page you must the offset/skip keywords.
Normally next and prev functionality using a good search gives good enough user experience :)
If you want to get to the previous page, remember the low bound for id as well as the high bound.
To scroll 3 pages ahead, use LIMIT 30 OFFSET 20 instead of LIMIT 10. To jump to page X, calculate the difference between X and the current page and multiply that difference with the number of rows per page.
It's all pretty straightforward.

Custom Pagination in datatable

I have a web application in which I get data from my database and show in a datatable. I am facing an issue doing this as the data that I am fetching has too many rows(200 000). So when I query something like select * from table_name;
my application gets stuck.
Is there a way to handle this problem with JavaScript?
I tried pagination but I cannot figure how would i do that as datatable creates pagination for already rendered data?
Is there a way through which I can run my query through pagination at
the backend?
I have come across the same problem when working with mongodb and angularjs. I used server side paging. Since you have huge number of records, You can try using the same approach.
Assuming a case that you are displaying 25 records in one page.
Backend:
Get the total count of the records using COUNT query.
select * from table_name LIMIT 25 OFFSET
${req.query.pageNumber*25} to query limited records based on the page number;
Frontend:
Instead of using datatable, display the data in HTML table it self.
Define buttons for next page and previous page.
Define global variable in the controller/js file for pageNumber.
Increment pageNumber by 1 when next page button is clicked and
decrement that by 1 when prev button is pressed.
use result from COUNT query to put upper limit to pageNumber
variable.(if 200 records are there limit will be 200/25=8).
So basically select * from table_name LIMIT 25 OFFSET
${req.query.pageNumber*25} will limit the number of records to 25. when req.query.pageNumber=1, it will offset first 25records and sends next 25 records. similarly if req.query.pageNumber=2, it will offset first 2*25 records and sends 51-75 records.
There are two ways to handle.
First way - Handling paging in client side
Get all data from database and apply custom paging.
Second way - Handling paging in server side
Every time you want to call in database and get records according to pagesize.
You can use LIMIT and OFFSET constraints for pagination in MySQL. I understand that at a time 2 lacs data makes performance slower. But as you mention that you have to use JS for that. So make it clear that if you wants js as frontend then it is not going to help you. But as you mention that you have a web application, If that application is on Node(as server) then I can suggest you the way, which can help you a lot.
use 2 variables, named var_pageNo and var_limit. Now use the row query of mysql as
select * form <tbl_name> LIMIT var_limit OFFSET (var_pageNo * var_limit);
Do code according to this query. Replace the variable with your desire values. This will make your performance faster, and will fetch the data as per your specified limit.
hope this will helpful.

Filter data with API/SQL or with Angular?

I'm having a bit of hard time trying to decide how I should filter results in a small-ish Angular SPA with a couple of thousand hits per month:
Let's say I have a database table albums, which contains the download counts of each album in the field downloads. I want to show the Top 3 most downloaded albums in the view. Should I just use $http.get('api/albums'), which returns all albums in JSON, and then ng-repeat="album in albums | filter: showTop3".
Or is it better to filter them with SQL, for example $http.get('api/albums?show=top3'), where the key=value runs code that executes (as a crude example) something like SELECT * FROM albums ORDER BY downloads DESC LIMIT 3.
Is there any practical difference between those two in a small site? Speed or maintainability-wise?

Variable number of records per page in Yii

I'm trying to get variable number of records per page in Yii. The page size is dependent on the number of records that match a criteria, so It has to be changed on every AJAX request sent by Yii's pagination.
Here's a sketch of the exact problem:
Table Structure [pk, user_id, entry_date, entry_message]
The view has a CGridView with pagination enabled and pageSize set to 10. What I want to do is to show the entries from one date on one page. For example, page one should show the entries from today. Page two should show the entries from yesterday. So I presume that the limit and the offset should be variable, but I can't quiet understand how to change them.

MongoDB infinite scroll sorted results

I am having a problem trying to achieve the following:
I'd like to have a page with 'infinite' scrolling functionality and all the results fetched to be sorted by certain attributes. The way the code currently works is, it places the query, sorts the results, and displays them. The problem is, that once the user reaches the bottom of the page and new query is placed, the results from this query are sorted, but in its own context. That is, if you have a total of 100 results, and the first query display only 50, then they are sorted. But the next query (for the next 50) sorts the results only based on these 50 results, not based on the 100 (total results).
So, do I have to fetch all the results at once, sort them, and then apply some pagination logic to them or there's a way for MongoDB to actually have infinite scrolling (AJAX requests) with sorting applying to the results?
There's a few ways to do this with MongoDB. You can use the .skip() and .limit() commands (documented here: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-CursorMethods) to apply pagination to the query.
Alternatively, you could add a clause to your query like: {sorted_field : {$gt : <value from last record>}}. In other words, filter out matches of the query whose sorted value is less than that of the last resulting item from the current page of results. For example, if page 1 of results returns documents A through D, then to retrieve the next page 2 you repeat the same query with the additional filter x > D.
Let me preface this by saying that I have no experience with MongoDB (though I am aware that it is a NoSQL database).
This question, however, is somewhat of a general database one (you'd probably get more responses tagging it as such). I've implemented such a feature using Cassandra (another, albiet quite different NoSQL database), however the same principles apply.
Use the sorted-by attribute of the last retrieved record, and conduct a range search based on it in the database. So, assuming your database consists of the following set of letters:
A
B
C
D
E
F
G
..and you were retrieving 2 letters at a time, you'd retrieve A, B first. When more records are needed, you'd use B to conduct a range search on the set of letters in the database. In plain English this would be something like:
Get the letters that appear after B, limit the results to 2
From a brief look at the MongoDB tutorial, it looks like you have conditional operators to help you implement this.