如何/在哪里暂时如果不能在会话中存储的ActiveRecord的对象? [英] How/where to temporarily store ActiveRecord objects if not in session?

查看:112
本文介绍了如何/在哪里暂时如果不能在会话中存储的ActiveRecord的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重构具有结帐的过程,打几个ActiveRecord模型一个基于Rails的事件注册申请。理想情况下,对象不应该保存,除非结算完成后(付款成功处理)。我不能完全确定为什么这将是一件坏事,暂时序列化这些对象到会话中,但我看了个遍,其不错的魔力。至少有不同步的现有记录,因为不会有任何现有记录的风险。

I'm refactoring a Rails-based event registration application that has a checkout process that hits several ActiveRecord models. Ideally, the objects should not be saved unless checkout is complete (payment is successfully processed). I'm not entirely certain why it would be a bad thing to serialize these objects into the session temporarily, but I've read over and over that its bad mojo. At least there's no risk of getting out of sync with existing records, as there won't be any existing records.

我的问题是:

A)是它仍然有问题在会话中存储记录,即使他们没有在其他地方存在?即使我修改一个模型,我不能杀死所有的现有会话?

A) Is it still problematic to store records in the session even though they don't exist elsewhere? Even if I modify a model, can't I kill all existing sessions?

B)如果可能会造成问题,我应该怎么临时存储对象?我应该保存和使用一个布尔标志,表示永久与临时身份?然后cron发出的脚本淘汰陈旧的临时对象?

B) If it may cause problems, how should I temporarily store objects? Should I save them and use a boolean flag to indicate permanent vs. temporary status? Then cron a script to weed out stale temporary objects?

思考?

推荐答案

你为什么不把它们存储在数据库中,但你知道的方式他们是不完整?

Why don't you store them in the database, but in a way you know they are "incomplete"?

例如,您可以添加 added_to_cart_at 日期时间字段。当产品被添加到购物车中,在保存记录并设置该字段的值。然后,如果用户完成购买,则清除该字段和你的产品关联到一个数量级。

For example, you can add a added_to_cart_at datetime field. When the product is added to the cart, you save the record and you set the value for that field. Then, if the user complete the purchase, you clear the field and you associate the product to an order.

要清除陈旧的记录,您可以设置每天cron来删除所有记录,其中 added_to_cart_at 早于1.day.ago。

To clear stale record, you can setup a daily cron to delete all records where added_to_cart_at is older than 1.day.ago.

这篇关于如何/在哪里暂时如果不能在会话中存储的ActiveRecord的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆