Monday, October 29, 2012

[Rails] Saving values to the db as metric but displaying/editing in imperial.

I have a requirement to store values in a database as unitless decimals(12,9) which will be presumed to be metric values. For display/editing purposes I need the user to be able to choose their input method -- no mixing and matching on a form, just a simple User#current_units call to determine the expected units.

For example.

item:
  name: item1
  diameter: 150
  width: 6

When User#current_units is "mm" all of my form fields are straight forward... 
f.label "Diameter"
f.text_field :diameter

My issue is when a current_units is "in", what's a good method for doing all the translation? The text_field for diameter should read "5.9055". If a user altered it to read "6" then it should save back to the db as "152.400000000"

I've tried everything from the ruby-units gem to composed_of (which is practically useless with rails 4 getting rid of it). storing the units in the db is a no go only because I need to be able to search for "items where diameter > x and diameter < y"

If anyone has any theories on a good starting point I'd love to elaborate further on what I've tried and failed with.

Thanks,
- FJM

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/G0EjqYKGLYMJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate