title: Rails: How do I validate model based on parent's attributes?
body: <p>I have three models named Metric, Entry, and Measurement:</p>
  
  <pre><code>class Metric &lt; ActiveRecord::Base
    has_many :entries
    attr_accessible :name, :required_measurements, :optional_measurements
  end
  
  class Entry &lt; ActiveRecord::Base
    belongs_to :metric
    has_many :measurements
    attr_accessible :metric_id
  end
  
  class Measurement &lt; ActiveRecord::Base
    belongs_to :entry
    attr_accessible :name, :value
  end
  </code></pre>
  
  <p>They are associated, and I can create nested instances of each this way:</p>
  
  <pre><code>bp = Metric.create(:name =&gt; "Blood Pressure", :required_measurement_names =&gt; ["Diastolic", "Systolic"])
  
  bp_entry = bp.entries.create()
  
  bp_entry.measurements.create({:name =&gt; "Systolic", :value =&gt;140}, {:name =&gt; "Diastolic", :value =&gt;90})
  </code></pre>
  
  <p>How do I validate the measurements for blood pressure based on the <code>:required_measurement_names</code> attribute in the Metric model? For example, how would I ensure that only "Systolic" and "Diastolic" are entered as measurements?</p>
  
  <p>Is there a better way to about setting up these associations and validations?</p>
  
  <p>Thanks!</p>