Rails 中只有一个具有给定状态的对象全局可用
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何 acts_as_state_machine (aasm) 宝石似乎都比重新实现更好。
Any of the acts_as_state_machine (aasm) gems seems like a better idea than re-implementation.