Forum: Ruby on Rails rails memcached dalli Marshalling error for key

Posted by Pavlo Shabat (Guest)
on 2012-12-25 13:16
(Received via mailing list)
I have such action in my controller:

def my

      @user = Ads::User.find current_user.id

      @postings = 
Rails.cache.fetch("@user.postings.includes(:category)") do

        @postings = @user.postings.includes(:category)

      end

end

I'm trying to cache @postings and get such error:

Marshalling error for key '@user.postings.includes(:category)': can't 
dump
anonymous class #<Module:0x000000048f9040>

You are trying to cache a Ruby object which cannot be serialized to
memcached.

If I try to cache @postings without includes there are no errors. Can't
figure out what is the problem
Posted by Frederick Cheung (Guest)
on 2012-12-25 13:20
(Received via mailing list)
On Dec 25, 11:09am, Pavlo Shabat <pavlo.sha...@gmail.com> wrote:
>    end
>
> end
>
> I'm trying to cache @postings and get such error:
>

This would try and cache the activerecord::relation object rather the
actual query results.
You need to force execution of the query, ie

Rails.cache.fetch("@user.postings.includes(:category)") do
  @user.postings.includes(:category).to_a
end

Fred
Posted by Jordon Bedwell (Guest)
on 2012-12-25 17:21
(Received via mailing list)
On Tue, Dec 25, 2012 at 5:09 AM, Pavlo Shabat <pavlo.shabat@gmail.com> 
wrote:
> You are trying to cache a Ruby object which cannot be serialized to
> memcached.

What is "includes" and what does it return? It should be a String,
Hash ({}), Array ([]), Fixnum (1), Range (1..2) or Float (1.0) for
Dalli to not bitch about Marshaling. Otherwise you have to build your
own Marshals for the object which might or might not be worth it.
Posted by Dheeraj Kumar (Guest)
on 2012-12-25 17:24
(Received via mailing list)
includes performs a join, and returns a ActiveRecord::Relation object. 
Just call .to_a like Frederick Cheung says, and Dalli will be happy :)

--
Dheeraj Kumar
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.