Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when attempting to cache #483

Open
antarr opened this issue Feb 13, 2022 · 2 comments
Open

Error when attempting to cache #483

antarr opened this issue Feb 13, 2022 · 2 comments

Comments

@antarr
Copy link

antarr commented Feb 13, 2022

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
@nertzy
Copy link
Collaborator

nertzy commented Feb 13, 2022

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.

@antarr
Copy link
Author

antarr commented Feb 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants