TypeError シングルトンはダンプできません
概要
私は Rails 4 プロジェクトに取り組んでおり、キャッシュを行うために readthis_store gem を使用しています。次のコードがありますが、
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
私のコントローラーには、block_reason メソッドを呼び出す次のメソッドインデックスがあります。
class Private::V1::JobsController < Private::V1::ApiController
def index
response = @current_user.block_reason.try(:to_h)
json_success(nil, response)
end
end
しかし、API呼び出しを行うと、エラーが発生します
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'
エラーが readthis_store gem で発生しており、シングルトンとしてキャッシュされたオブジェクトを定義する Marshal.dump に関係していることはわかっています。
しかし、他にこの問題を解決する方法がわかりません。さまざまなオプションを試しましたが、すべて無駄でした。
これに遭遇して Rails 4 で解決できた人がいるかどうかはわかりません。
解決策
キャッシュされているすべてのコンテンツが実際にシリアル化可能であることを確認してください。コードからわかることは、次の中に 2 つのドットがあることです。
...
status: self..status
...