我正在为我的网站构建一个购物车,但我在使用 rails session 和 ActiveRecord 对象时遇到了问题:
Shopping Cart 实际上是一个带有一些 OrderItems 的 Order 对象。
将 Order 对象添加到会话后,Dalli 开始引发此异常:
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'
谷歌搜索,我发现 ActiveRecord 不能存储在会话中。
那么,在会话中存储这些对象的最佳方式是什么?我必须为这些类实现序列化/反序列化方法吗?
谢谢!
编辑1:
记住 Order 对象还没有持久化!