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

Float numbers are formatted to integer #73

Open
elamonica opened this issue Aug 31, 2022 · 2 comments
Open

Float numbers are formatted to integer #73

elamonica opened this issue Aug 31, 2022 · 2 comments

Comments

@elamonica
Copy link

The problem appears when you have a numeric column with float data.
When you get the model, the attribute is formatted as integer instead of float.

@mathieulb
Copy link

What just happened to me is that when I do Parse.auto_generate_models!, and try to read a field whose type is Number, I get a value rounded downwards, e.g. 2.8 (Float) becomes 2 (ruby Integer). Furthermore, attempting to set it to Float 2.8, and then saving it, sets the db field to 2. Actually, even just doing set("stuff", 2.8) then get("stuff") returns 2.

Is that what happens to you ?

@mathieulb
Copy link

also :

MyClass.class_eval {property :my_field, :float, field: "My_Field"}

warns :

Property MyClass#my_field already defined with data type :float. Will be ignored.

when the actual type it's already defined with, is :integer ; generally it reports whatever type I write in the statement, instead of the existing one. See lib/parse/model/core/properties.rb :

if (self.fields[key].present? && BASE_FIELD_MAP[key].nil?) || (self < Parse::Object && BASE_FIELD_MAP.has_key?(key))
  warn "Property #{self}##{key} already defined with data type :#{data_type}. Will be ignored."
  return false
end

also wrong documentation for def attributes, copy-pasted from def enums :

# @return [Hash] the fields that are marked as enums.

I just created this workaround :

module Parse::Properties::ClassMethods
  def remove_property key, **opts
    key = key.to_sym
    parse_field = opts[:field].to_sym || key.to_s.camelize(:lower)
    fields.delete key
    fields.delete parse_field
    attributes.delete parse_field
    remove_method parse_field
    remove_method :"#{key}_increment!"
    remove_method :"#{key}_decrement!"
  end
end
class MyClass
  remove_property :my_field, field: :My_Field
  property :my_field, :float, field: :My_Field
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants