Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding validations to rails aasm state

in state_machine I used to do

state :cancelled do
  validates_presence_of :user
end

it would automatically cancel the transition if user was not present.

How do we add similar validations to specific states in aasm?

like image 868
Raz Avatar asked Mar 02 '15 15:03

Raz


1 Answers

I can offer 2 options:

the first:

validates_presence_of :sex, :name, :surname, if: -> { state == 'personal' }

the second

event :fill_personal do
  before do
    instance_eval do
      validates_presence_of :sex, :name, :surname
    end
  end
  transitions from: :empty, to: :personal
end
like image 169
eugene_trebin Avatar answered Sep 24 '22 14:09

eugene_trebin