Antonin Mrchd
Antonin Mrchd

Reputation: 666

Ruby on rails - slow 200 records query

I work on a travel app, with a page that load 250 countries, with one image per country. In production, this page is really slow, and I want to know if someone have a solution to handle loading records 20 per 20 for example, or to have a more faster loading ?

Currently, here is my basic query :

@worlds = World.all.sort_by(&:count_visited).reverse

Thank you !

EDIT

I can't use a paginate gem because I'm using list.js, to easily find a country. So I need that all my records being on the same page.

Upvotes: 1

Views: 96

Answers (1)

Shiko
Shiko

Reputation: 2624

You have 2 ways to solve your problem:
Solution #1:
Compress the resolution of the images using one of below:
A) Using any image Editor
B) Using paperclip-compression gem library
https://github.com/emrekutlu/paperclip-compression

Solution #2:
If you want to keep the current resolution, then you can use will_paginate gem library to show 10,20 or any number of records per page:
https://github.com/mislav/will_paginate

Controller:

## perform a paginated query:
@posts = Post.paginate(:page => params[:page])

# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)

ERB file:

## render page links in the view:
<%= will_paginate @posts %>

Upvotes: 1

Related Questions