Search code examples
ruby-on-railsjsonassociationsjbuilder

Rails include through Jbuilder partial


I have a jbuilder show that renders a partial:

json.contents @item_trackers, partial: 'item_trackers/item_tracker', as: :it

In this partial I access my pages_trackers association through the it variable:

json.pages it.pages_trackers.order("created_at ASC")

The problem is that when the request is fired, it loads every page_tracker that belongs to an item_tracker (one request for each).

I've tried to include the pages_trackers model in my controller: item_trackers.includes([:pages_trackers])

But it still loads every page_tracker and I have the "unused eager loading" message:

Unused Eager Loading detected ItemTracker => [:pages_trackers] Remove from your finder: :includes => [:pages_trackers]

Can someone help me with this?


Solution

  • I found out what the problem was: When I was calling the order method in the partial, it forced the requests to be fired again. So I changed the SQL method order for the Ruby counterpart: sort_by(&:created_at)