Casecommons/pg_search

Error when attempting to cache

antarr opened this issue · 2 comments

I'm trying to cache queries on the most frequently accessed page on my site. I'm getting an error when adding Rails.cache. The page loads fine without caching.

error

can't dump anonymous class #<Module:0x00007fc4d5b52ba0>

dashboard_controller.rb

  def news_feed_items
    Rails.cache.fetch("news_feed_items_#{filter_as_json}_#{items_page}", expires_in: 1.hour) do
      PgSearch.multisearch(current_user_states.join(' ')).
        where('updated_at >= ?', number_of_days.days.ago).
        order(:updated_at).
        page(items_page).
        per(5)
    end
    # TODO: Add a filter sort by popularity
  end

I'm guessing that it's not possible to cache an instance of ActiveRecord::Relation, which is what PgSearch.multisearch returns. In any case, caching a Relation before it has loaded would mean caching the object that represents the query, but not caching its results, which would be of little value.

I would try chaining #to_a to the end of the relation to get back an array of records. Presumably then those records could be cached.

#to_a didn't work but but #load does. However, not I get an error undefined method total_pages when paginating with == paginate items