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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With