Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Validation not running when including multiple states in `state` block #198

Closed
sfsekaran opened this Issue · 3 comments

4 participants

@sfsekaran

I'm including my real state machine from a module (with ActiveSupport::Concern). I'm not sure if that's related to the issue. I'm using ActiveRecord.

This is an example, the real machine has a lot more logic.

    state_machine :state, initial: :unpublished do
      event :publish do
        transition :unpublished => :waiting_for_moderation, if: :can_moderate?
        transition :unpublished => :waiting_for_junk, if: :can_junk?
      end

      state :waiting_for_moderation, :waiting_for_junk do
        validate :custom_validation_method
      end
    end

    def custom_validation_method
      # THIS NEVER GETS EXECUTED AT ALL
      self.errors.add(:account, "Has not been verified or something.")
    end

However, when I add two separate state blocks, calling validate :custom_validation_method in each of them, the validation gets called.

@alex-tan

I believe I ran into this problem as well. I think my workaround was to do:

validate { |m| m.custom_validation_method }
@frahugo

Same problem, with a very simple ActiveRecord class (not concern). The workaround does work.

@obrie
Owner

Hey guys -

This is actually documented in the ActiveRecord integration @ http://rdoc.info/github/pluginaweek/state_machine/master/StateMachine/Integrations/ActiveRecord under the "Validations" section. This is due to a constraint in ActiveRecord's validation framework.

@obrie obrie closed this
@obrie obrie was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.