Code Discussions: Modeling ...

## Background class Member < ActiveRecord::Base has_many :readings end class Metric < ActiveRecord::Base has_many :metric_attributes end class MetricAttribute < ActiveRecord::Base belongs_to :metric has_many :readings end class Reading < ActiveRecord::Base belongs_to :member belongs_to :metric_attribute end Example: Metric: Cholesterol MetricAttribute: HDL Member: me Reading: HDL - 64 ## The Problem There is a view in the application where we start with a list of metrics and need to output the latest reading for an attribute and the current user. The initial conversation:

Justin Marney

As I see it there are three ways to get at the reading. I can go from the attribute:


or the member:


or the Reading class:

Reading.for_member_and_attribute(member, attribute)

Assuming some scopeage gets me the latest. That aspect is not my concern ATM, but the things that hangs me up is, which do I choose?

Patrick Reagan
You're looking for the right API, yea?
So yea, what are the tradeoffs, if any, to choosing one over the other? Is there an appropriate choice?
I have 2 opinions -- one of which i can justify and another I cannot.
Is the second one that you can't trust a woman?

3 then. But, I would come from the member:




Based on what you describe, it sounds like getting this reading is a concern of member.

Patrick Reagan

Patrick is development director in Viget's Boulder, CO, office. He writes clean Ruby code and automates system infrastructure for clients such as Shure and Volunteers of America.

More articles by Patrick