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

Coercion has changed from 3.x to 4.0

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 4.0.0 final
    • Component/s: None
    • Labels:
      None

      Description

       ruby
      require 'mongoid'
       
      class Foo
        include Mongoid::Document
       
        field :flag, type: Boolean
      end
       
      foo = Foo.new(flag: "true")
      foo.flag
      

      In 3.x, foo.flag is coerced to a Boolean and stored as such.

      1.9.3-p385 :022 >   foo = Foo.new(flag: "true")
       => #<Foo _id: 526a73633b5552cd6b000001, flag: true> 
      1.9.3-p385 :023 > foo.flag
       => true 
      

      In master, foo.flag is not coerced to a Boolean and stored as a string.

      1.9.3p385 :009 >   foo = Foo.new(flag: "true")
       => #<Foo _id: 526a738264626c34fe000000, flag: "true"> 
      1.9.3p385 :010 > foo.flag
       => "true" 
      

      Is this by design? How can I force coercion into a type?

      My typical scenario is a web-based POST, and having booleans become booleans makes it really simple, a query string "flag=true" becomes a "true" string by default.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: