Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails app. Collect items from several models and show this paginated list of items on main page

In Rails app I have three models (Posting, Shop::Items and Directory::Items). Every model has relations with other models (User, Category, City ...). All this info I have to show on main page. It should look like paginated list of items. I'm using postgres for DB.
We can write such code in our controller:

@postings = Posting.includes(:category, :subcategory, :user, :city).all
@directory_items = Directory::Item.includes(:category, :user, :city).all
@shop_items = Shop::Item.includes(:category, :user, :city, :phone).all

and sort this results. But it is not the best way.

Second solution is to write custom query ommitting ActiveRecord. But this query in really large and it will be hard to support it (we have a lot of joins there)
What is the best solution for that with the smallest amount of DB queries? (This is main page, so it should work really fast).

like image 264
jizak Avatar asked Nov 03 '22 06:11

jizak


1 Answers

There are some solutions which are not fast in here.

Alternatively you can use sphinx with thinking sphinx gem. This way it will be fast and clean.

ThinkingSphinx.search classes: [Posting, Shop::Items, Directory::Items], page: params[:page] || 1, per_page: 10
like image 172
emrahbasman Avatar answered Nov 15 '22 05:11

emrahbasman