Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latitude and longitude should be float instead of string. #246

Merged
merged 1 commit into from
May 20, 2018

Conversation

zeitnot
Copy link
Contributor

@zeitnot zeitnot commented Aug 22, 2014

Mongodb gives an exception when set coordinates as string. So they should be set as float. Globally coordinates should be set as float by default.

Copy link
Member

@vbrazo vbrazo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing 🥇

Your changes look ok. We should rebase before merging.

@vbrazo vbrazo merged commit 54ca5ef into faker-ruby:master May 20, 2018
@zeitnot
Copy link
Contributor Author

zeitnot commented May 21, 2018

👍

@stephendolan
Copy link

stephendolan commented Jul 31, 2018

This caused a very difficult bug for me to track down in my Rails 5.2 application. I'd like to leave a comment here for any other unfortunate souls who run into an error when upgrading from 1.8.7 -> 1.9.1.

I have a latitude column of type :decimal:

FieldEntry.new.column_for_attribute('latitude')
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x00007fcecb2561f0
 @collation=nil,
 @comment=nil,
 @default=nil,
 @default_function=nil,
 @max_identifier_length=63,
 @name="latitude",
 @null=false,
 @sql_type_metadata=
  #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fcecb256308
   @limit=nil,
   @precision=nil,
   @scale=nil,
   @sql_type="numeric",
   @type=:decimal>,
 @table_name="field_entries">

Note the behavior that this merge request introduces because of the way Rails parses floats vs. strings to a BigDecimal value:

FieldEntry.new(latitude: 29.081408856861657.to_s).latitude
=> 0.29081408856861657e2
FieldEntry.new(latitude: 29.081408856861657.to_f).latitude
=> 0.290814088568617e2

This broke all of the comparisons in my RSpec tests throughout my application because of the incorrect rounding that Rails was doing as a result of getting a float.

The solution was to chain to_s onto my Faker::Address.latitude/longitude calls.

@atomaka
Copy link

atomaka commented Aug 10, 2018

Echoing @stephendolan, this was difficult to track down and I never guessed Faker was responsible. Perhaps this should be noted in the changelog?

davidmorton0 pushed a commit to davidmorton0/faker that referenced this pull request Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants