title: How to validate an instance with dynamic code in rails 3.2 model?
body: <p>We need to validate a model <code>instance</code> with dynamic code based on the status of the instance. The status of the instance is returned by instance field <code>wf_state</code>. </p>
  
  <p>Here is what we use in the model:</p>
  
  <pre><code>wf_validation_def = find_config_const('validation_string_for_status_new', 'model_name') if (wf_state == 'new')
  eval(wf_validation_def) if (wf_state == 'new')
  </code></pre>
  
  <p>Here the validation string returned in <code>wf_validation_def</code> by <code>find_config_const</code> when status is <code>new</code>. The validation string is:</p>
  
  <pre><code>"validates :unit_price, :presence = true, :numericality =&gt; {:greater_than =&gt; 0} "
  </code></pre>
  
  <p>However the above code returns error in rspec:</p>
  
  <pre><code>.../activerecord-3.2.12/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined local variable or method 'wf_state'
  </code></pre>
  
  <p>Tried <code>self.wf_state</code> and the error is the same. What's wrong with the code above?</p>