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

No data transformation performed on Mongoid fields before save?

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      According to the documentation for Mongoid 3:

      Even though MongoDB is a schemaless database, most uses will be with web applications where form parameters always come to the server as strings. Mongoid provides an easy mechanism for transforming these strings into their appropriate types through the definition of fields in a Mongoid::Document.

      If I create a document with the following definition:

      require 'mongoid'
      class Doc
         include Mongoid::Document
         field :val,  type: Array
      end
      

      ... and then do this:

      d = Doc.new
      d.val = "1,2,3"
      d.save!
      

      ... I end up with this:

      #<Doc _id: 52fa6d5ac6992f8bc8000003, val: "1,2,3">
      

      Why is the value not being transformed to an Array before the document is saved? This seems to go against the very reason for defining field types in the first place.

      Am I missing some option I need to toggle or additional validation DSL I need to include in order to get this working? At the very least, I would expect an error to be thrown when I try to set a field to a value of a different type and then attempt to save.

      Thanks for any help, in advance.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            adamlwatson adamlwatson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: