0

I'm making a JSON API for my iOS app, and (even though I format it on the client) I would like to store my lat/long data in the format it will be seen on the client.

My current solution

class Location < ActiveRecord::Base
    before_save :format_coordinate_precision

    def format_coordinate_precision
    lati = "%.8f" % self.latitude
    self.latitude = lati.to_f
end

Gives me a guaranteed 8 digits of precision, but omits trailing zeros. Plus this doesn't look like the most production quality approach to this. There's the ActionView::Helpers::NumberHelper number_with_precision but that seems to be for use in .erb view files.

user
  • 3,388
  • 7
  • 33
  • 67
  • I'm not familiar with Ruby, but that looks like a printf()-style format specification. If that's the case, `lati = "%8.8f" % self.latitude` should get you the trailing zeroes you want. – Mark Jan 06 '14 at 07:47
  • In most languages that would work, but Ruby seems hellbent on ridding of those trailing zeros. (I tried, same result) – user Jan 06 '14 at 08:01
  • @user, see my answer below for a solution, along with a discussion of caveats of this approach. – zeantsoi Jan 06 '14 at 08:44
  • Any luck with this so far? – zeantsoi Jan 07 '14 at 05:06
  • Yep, check your answer. – user Jan 07 '14 at 06:24
  • This answer does the trick: http://stackoverflow.com/questions/7028688/trailing-zeroes-not-going-into-database . This was more of a SQL question than ActiveRecord. – user Jan 07 '14 at 07:01

1 Answers1

1

You're correct that the number_with_precision method is intended for use in views, as are all NumberHelper functions. However, if you're willing to bend the rules (which is a whole different discussion), you can invoke these functions outside of views by explicitly including the NumberHelper module:

include ActionView::Helpers::NumberHelper

number_with_precision(1234567890.314159, precision: 10)
#=> 1234567890.3141590000

In general, it's possible to invoke helper functions outside their respective layer by including their helper module. While the above example seems innocuous enough, I would recommend caution with importing helpers — it's typically a violation of Rails convention to utilize MVC functions outside of their intended scope-of-use.

zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • Thanks, this works as expected. It still irks me a little bit though, I'd be really disappointed if this is the most optimal solution rails has to offer. For now though, it does the trick. – user Jan 07 '14 at 06:26
  • Yeah... Rails has a lot of useful conventions, but seems to be missing some obvious ones, too. Since this answer worked, would you kindly consider upvoting/accepting it as correct? – zeantsoi Jan 07 '14 at 06:28
  • Actually it only works in the console, it seems ActiveRecord ignores the formatting. – user Jan 07 '14 at 06:44
  • 1
    It _is_ working... what's happening is that, by re-casting the float using `to_f`, you're truncating the trailing zeroes. So if you're trying to save to a `float` type attribute, it'll _always_ have the trailing zeroes truncated. The only alternative I can envision would be to save the float as a string, then recast it when you need it. – zeantsoi Jan 07 '14 at 06:53