Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4251662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:41:30+00:00 2026-05-21T04:41:30+00:00

I have three models named Metric, Entry, and Measurement: class Metric < ActiveRecord::Base has_many

  • 0

I have three models named Metric, Entry, and Measurement:

class Metric < ActiveRecord::Base
  has_many :entries
  attr_accessible :name, :required_measurements, :optional_measurements
end

class Entry < ActiveRecord::Base
  belongs_to :metric
  has_many :measurements
  attr_accessible :metric_id
end

class Measurement < ActiveRecord::Base
  belongs_to :entry
  attr_accessible :name, :value
end

They are associated, and I can create nested instances of each this way:

bp = Metric.create(:name => "Blood Pressure", :required_measurement_names => ["Diastolic", "Systolic"])

bp_entry = bp.entries.create()

bp_entry.measurements.create({:name => "Systolic", :value =>140}, {:name => "Diastolic", :value =>90})

How do I validate the measurements for blood pressure based on the :required_measurement_names attribute in the Metric model? For example, how would I ensure that only “Systolic” and “Diastolic” are entered as measurements?

Is there a better way to about setting up these associations and validations?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:41:30+00:00Added an answer on May 21, 2026 at 4:41 am

    Looks like entry is a join model between Measurement and Metric. so it makes sense for your validation to go there.

    To ensure that all metrics required metrics are covered

    def validate_required_measurements
      metric.required_measurements.each{ |requirement| 
         unless (measurements.map{|measurement| measurement.name}.includes requirement)
           errors.add_to_base "required measurement #{requirement} is missing"  
         end
      }
    end
    

    To ensure that only accepted metrics are included (assuming optional metrics are accepted too).

    def validate_accepted_measurements
      measurements.each{ |measurement| 
         unless ((metric.required_measurements + metric.optional_measurements).include measurement.name )
           errors.add_to_base "invalid measurement #{measurement.name} for metric"  
         end
      }
    end
    

    Putting it all together add the above and following to the Entry model

    validate: validate_accepted_measurements, :validate_required_measurements
    

    N.B. my Ruby is a little rusty so this isn’t guaranteed to work after copying and pasting, but it should get you close enough to fix the syntax errors that slipped through.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
According to an example , I have three models: class User(models.Model): name = models.CharField(max_length=128)
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have three models: User Award Trophy The associations are: User has many awards
I have three models. User, Product, and Season. I used a standard has many
I have three models that I want to interact with each other. Kase, Person
Lets say I have three models/tables: operating_systems , words , and programming_languages : #
I'm using rails 3.0 and MySql 5.1 I have these three models: Question, Tag
The queryset I've constructed is incorrectly leaving out some items. I have three models
I have three models, Event, Address and County, that are set up like this.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.