简体   繁体   中英

Only one object with given state available globally in Rails

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?

Any of the acts_as_state_machine (aasm) gems seems like a better idea than re-implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM