I recently started using memcached with my mongoid database for Tathkarti. The site uses a lot of records, and I needed to cache collection of records. So I first tried something like this:
Rails.cache.fetch('teams', :expires_in => 1.day) do Team.all end
This resulted in the following error:
“You are trying to cache a Ruby object which cannot be serialized to memcached.”
This is because Mongoid, and Rails 3 in general, lazy loads all queries. That is, initialization of an object is delayed until it is needed. Hence, Team.all in that case would only return Mongoid::Criteria in my case, and would not result in execution of the query and the return of the results you want cached. This may not be the best solution, but what I did was simply call .entries on the query like this.
Rails.cache.fetch('teams', :expires_in => 1.day) do Team.all.entries end