1

Writing an application using Mongoid 3.1 and Sinatra in Ruby 1.9.3. I have a model called Order that has_many Items. Whenever I try to append an Item to an Order.items, I run into problems. I have the following route, summed up slightly:

    order = session[:user].get_order(Time.now)
    order.items << Item.new
    order.save
    "Hi, mom!" # Garbage page so that I know nothing else is called.

Doing that once is okay; doing it twice causes the following error:

    Warning! Rack::Session::Cookie data size exceeds 4K.
    Warning! Rack::Session::Cookie failed to save session. Content dropped.

I've been banging my head against the wall trying to get it to stop doing this. Why is the session loading all my items? Am I not using the has_many relationship correctly?

4

1 回答 1

1

你的User模型可能has_many :orders。Ruby 可能正在调用Marshal.dump将您的用户对象转储到 cookie 中。你可以想象这可能会变得巨大。您应该执行以下操作:

  • 仅将 user_id 存储在会话中。
  • 将会话存储在服务器端而不是 cookie 中。

您需要使用不同的中间件来存储会话服务器端。有关将会话存储在 memcache 中的示例,请参阅此页面。由于您已经在使用 mongo,您可以使用Rack::Session::Mongo

即使您没有使用 Rails,Rails 会话安全指南也很有用。[关联]

于 2013-05-18T02:43:26.847 回答