-1

I'm building an Shopping Cart for my site and I having one trouble with rails session and ActiveRecord Objects:

Shopping Cart is actually one Order object with some OrderItems.

After add Order object to session, Dalli started raise this exception:

Marshalling error for key 'sessions:9e67886e826819a3213df4f4c89db6e0': can't dump hash with default proc
You are trying to cache a Ruby object which cannot be serialized to memcached.
/home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/server.rb:277:in `dump'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/server.rb:277:in `serialize'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/server.rb:164:in `set'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/server.rb:48:in `request'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/options.rb:18:in `block in request'
    /home/rodrigo/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/options.rb:17:in `request'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/client.rb:251:in `perform'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/dalli/client.rb:120:in `set'
    /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@netsabe-ecommerce/gems/dalli-2.1.0/lib/action_dispatch/middleware/session/dalli_store.rb:50:in `set_session'

Googling, I found that ActiveRecord can not be stored on session.

So, how is the best way to store this objects on session? I will have to implement serialize/deserialize methods for these classes?

Thank you!

EDIT 1:

Remembering that Order object isn't persisted yet!

1 Answer 1

7

Storing objects in the session is a bad idea.
Storing Objects in a Session in Rails
Better to store the id of the object so you can always check if the object in on the latest state.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but Order object is not persisted on database yet.
@Rodrigo: It doesn't have to be persisted, you'll get a default proc pretty much as soon as you think about an AR object: stackoverflow.com/a/6392704/479863
but that will always run the database query.. if i store object to session i will save that queries .. ?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.