On Tue, Jan 17, 2012 at 00:06, Dustin Frazier <[email protected]> wrote:
> I'm working on a Rails 3 app that requires a few fields that can > be given in multiple units of measure, e.g. miles, kilometers, etc. > for distances, miles-per-hour, meters-per-second, etc. for speeds, > and so on. I'm curious how others have modeled these types of > compound datatypes in Rails. I haven't done this in Rails, but I've done somewhat-similar things in C++. There, I made a set of classes including Unit (e.g. meter, joule, etc.) and Measure (e.g. 3 meters, -12.3 joules, etc., a combination of a numeric value and a reference to a Unit), which is what I think you mean by UnitValue. Units could be either "base", or defined in terms of a scaling or combination (product or quotient) of other Units. Under the hood a Unit had a list of base Units it depended on (each with a power), plus a scale factor (usually not used except for metric/English conversions). I think you're on the right track with creating such classes. Your Units are probably simple and constant enough that they don't need to be database-backed. I'd still have an id data-member on them though, for easy handling via AR-type methods. Have the user enter the value in a textbox and choose the unit from a dropdown list (collection_select), which determines the unit_id in the form. You can have a has_one if you like, for convenience (myMeasure.unit), but you don't need a belongs_to -- after all, each Unit is likely to be referenced from many UnitValues. > What I'd really like is to have my UnitValue object flattened/embedded into > the > main object table as multiple fields, but I'm not sure if that's possible in > Rails at all. Depending on what you mean, that may be exactly what I describe above. Am I close? -Dave -- Dave Aronson, President, Dave Aronson Software Engineering and Training Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote) DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!) Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

