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

Field defaults should behave more like SQL column defaults

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Given the following models

      class House
      include Mongoid::Document
      field :stories, :type => Integer, :default => 1
      embeds_many :rooms
      end

      class Room
      include Mongoid::Document
      field :doors, :type => Integer, :default => 1
      embedded_in :house, :inverse_of :rooms
      end

      And given the following documents

      Mongoid.database[:houses].insert({})
      Mongoid.database[:houses].insert({ 'stories' => nil, 'rooms' => [{}] })

      Then I would expect

      >> House.first.stories #=> 1
      >> House.first.rooms #=> []
      >> House.last.stories #=> nil
      >> House.last.rooms.first.doors #=> 1

      Instead the result is the following

      >> House.first.stories #=> nil
      >> House.first.rooms #=> []
      >> House.last.rooms.first.doors #=> nil

      This allows Mongoid to support the following features offered by column defaults in relational databases and mirror ActiveRecord capabilities:

      • Defaults are retroactive to records predating the field
      • Any change to the default applies to any record that has not had the field explicitly set

            Assignee:
            Unassigned Unassigned
            Reporter:
            cgriego Chris Griego
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: