ruby on rails 3 - Contextual validations in ActiveRecord using with_options and an array -
moving datamapper activerecord , 1 cool feature using contextual validations , using with_options pass array below:
with_options when: [:started, :completed] |v| v.validates_with_method :has_data, method: :check_data_started? end
which fire validations within block if .valid?(:started)
or .valid?(:completed)
called.
is there way active record, tried:
with_options on: [:started, :completed] |v| v.validate :check_data_started? end
which looks blows error saying can't pass array within with_options call, it's expecting 1 context:
/home/vagrant/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:414: syntax error, unexpected '[', expecting tstring_content or tstring_dbeg or tstring_dvar or tstring_end ...ue && (validation_context == :[:started, :complet...
has done before?
figured out. if else runs problem, please see below solution:
with_options if: -> {[:started, :completed].include?(validation_context)} |v| v.validates_with_method :has_data, method: :check_data_started? end
Comments
Post a Comment