Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3572

Unexpected result when casting to Integer

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Hi,
      I am using mongoid 4.0.0.alpha2 (commit `e7aef3bc79`)
      I have a model with a column with the type Integer.

      class FeedProduct
        include Mongoid::Document
        field :e,   as: :ean,                   type: Integer
      end
      

      When I set the attribute with a String that has a dot, Mongoid sets it to 0, which at least to me is unexpected behaviour.:

      >> FeedProduct.new(ean: "713132333822.")
      => #<FeedProduct _id: fff7cce20000eff7d3ccbbf8, e(ean): 0>
      

      If the string has just numbers it works correctly.

      This solves the problem:

        def ean=(new_ean)
          write_attribute(:ean, new_ean.try(:to_i))
        end
      

      Is this a known issue? expected behaviour?

            Assignee:
            Unassigned Unassigned
            Reporter:
            eljojo eljojo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: