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

Closed
sfsekaran opened this Issue Jul 6, 2012 · 3 comments

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@alex-tan

alex-tan Oct 1, 2012

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

validate { |m| m.custom_validation_method }

alex-tan commented Oct 1, 2012

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

validate { |m| m.custom_validation_method }
@frahugo

This comment has been minimized.

Show comment
Hide comment
@frahugo

frahugo Jan 30, 2013

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

frahugo commented Jan 30, 2013

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

@obrie

This comment has been minimized.

Show comment
Hide comment
@obrie

obrie Feb 18, 2013

Member

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.

Member

obrie commented Feb 18, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment