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