Rails 中只有一个具有给定状态的对象全局可用

发布于 2024-12-22 03:06:21 字数 417 浏览 2 评论 0原文

我使用 Rails 3.1 和 Mongoid。

我使用 RESTful 控制器。

我有一个课程,让我们说一下描述。

这个类有一个名为 :state 的属性,有两个(也许有一天更多)有效值,例如 :active 和 :working。

我的客户希望有 N 个工作版本 (:working),但每次只有一个 :active 描述。这意味着当我#activate 描述时,旧的活动描述必须被停用。

问题是,我一直在使用回调方法(before_validation => :deactivate_previous_description)。只要我尝试激活工作副本就可以了。但是,当我对 :active 描述执行相同操作时,它会被停用,因为回调将其状态切换回 :working。

我做错了什么?有没有更好的方法来解决这个问题?

I use Rails 3.1 and Mongoid.

I use RESTful controllers.

I have a Class, let's say Description.

This class has an attribute, called :state with two (maybe someday more) valid values such as :active and :working.

My client wants to have N working versions (:working), but one and only one :active description at each time. This means that when I #activate a description, the old active one has to be deactivated.

The thing is, I've been using the callback approach (before_validation => :deactivate_previous_description). It is fine as long as I try to activate a working copy. But when I do the same to an :active description, it gets deactivated because the callback switches its state back to :working.

What am I doing wrong? Is there a better way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文