1

I am working on a Rails 4 project and using readthis_store gem to do my caching. I do have the following code though

I have user model with a method block_reason

class User < ActiveRecord::Base

  def block_reason
    Rails.cache.fetch([self.cache_key, __method__], expires_in: 1.hours) do
      {
          blocked: true,
          blocked_reason: reason,
          blocked_reason_text: reason_text,
          application_blocks: {
            status: self..status,
            name_check: self.name_check,
            id_check: self.id_check,
          }
       }
    end
  end
end

In my controller, i have the following method index which calls the block_reason method

class Private::V1::JobsController < Private::V1::ApiController
  def index
    response = @current_user.block_reason.try(:to_h)

    json_success(nil, response)
  end
end

But when i make the APi call, i get the error

TypeError: singleton can't be dumped
  from readthis/entity.rb:50:in `dump'
  from readthis/entity.rb:50:in `dump'
  from readthis/cache.rb:315:in `write_entity'
  from readthis/cache.rb:86:in `block in write'
  from connection_pool.rb:63:in `block (2 levels) in with'
  from connection_pool.rb:62:in `handle_interrupt'
  from connection_pool.rb:62:in `block in with'
  from connection_pool.rb:59:in `handle_interrupt'
  from connection_pool.rb:59:in `with'
  from readthis/cache.rb:346:in `block in invoke'
  from readthis/cache.rb:338:in `block in instrument'
  from active_support/notifications.rb:166:in `instrument'
  from readthis/cache.rb:338:in `instrument'
  from readthis/cache.rb:345:in `invoke'
  from readthis/cache.rb:85:in `write'
  from readthis/cache.rb:146:in `fetch'

I know the error is happening in the readthis_store gem and has something to do with Marshal.dump defining the object cached as a singleton.

But i do not know how else to solve this issue. I have tried various options but all to no avail.

Not sure if anyone has come across this and been able to solve it on Rails 4.

Kingsley Simon
  • 2,090
  • 5
  • 38
  • 84
  • Can you post the full error message? Is it just that one line, or is there more context? – Tom Lord Jan 12 '21 at 13:16
  • @TomLord updated but that is the bulk of the message – Kingsley Simon Jan 13 '21 at 06:29
  • Have you tried debugging that line, to see what the `singleton` actually is? I'm not sure what the cause/fix is based on the above; the first thing I'd try doing is dig around with a debugger. For example, *maybe* [this answer](https://stackoverflow.com/a/31412899/1954610) is relevant? – Tom Lord Jan 13 '21 at 10:42
  • Incidentally, checking the `readthis` [history](https://github.com/sorentwo/readthis/commit/553d45273d179ece53c723cb9b4a827a0c378d5b), I can see you're using a gem version that's *at most* `v1.3.0`, not the latest (`v2.2.0`) - even though the gem was last updated 3 years ago! So debugging this could be extra difficult in case you're missing any bug fixes from dependencies released over the years. – Tom Lord Jan 13 '21 at 10:42

0 Answers0