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

Coercion has changed from 3.x to 4.0

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

       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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: