-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
When you have allow_dynamic_fields set to false and in your database you have a field that isn't declared inside of an embedded field, initializing the embedded document will raise a NoMethodError exception. This is not the case if the unknown field is on the root document. In the following example, the Location class has a state_or_province field but the database already has a document with a state field.
class Location
include Mongoid::Document
embedded_in :bar
field :city, :type => String
field :state_or_province, :type => String
end
class Bar
include Mongoid::Document
field :name, :type => String
embeds_one :location
end
Mongoid.database['bars'].insert(:name => "Cheers", :location => { :city => "Boston", :state => "Massachusetts"})
Bar.first #=> #<Bar _id: 4da4f4a4d4ec9e695b000001, name: "Cheers", _id: BSON::ObjectId('4da4f4a4d4ec9e695b000001'), _type: nil>
Bar.first.location #=> NoMethodError: undefined method `state=' for #<Location:0x10b298c70>