简体   繁体   中英

Validating a finite state machine (using AASM) on Rails

I'm using AASM by Rubyist to build a 4-step wizard for an AR object. According to the state of the object, there are different validations that need to be done. What is the smartest way to validate an object according to it's state on a certain transition?

Use the :if option, and with_options to bunch 'em together.

class Post < ActiveRecord::Base
  with_options :if => proc {|r| r.signup_step > 2 } do |o|
    o.validates_presence_of :title
    o.validates_presence_of :yeah
  end
end

What exactly you ought to write in the if-proc depends on your implementation, obviously.

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