-
Type:
Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
On Mongoid 2.0.0 beta 20, when you have an embedded document and you do an only() without an embedded documents _id included, it'll just generate a new one every time, for example:
class Person
include Mongoid::Document
field :name, :type => String
embeds_one :location
end
class Location
include Mongoid::Document
field :address, :type => String
embedded_in :person, :inverse_of => :location
end
Followed by:
p = Person.new(:name => "Foo")
p.build_location(:address => "1234 Fake Street")
p.save
Would look something like:
{"_id": "4d22c9781f17fd8150000004", "name": "Foo", "location": {"_id": "4d22c9781f17fd8150000001", "address": "1234 Fake Street"}}
And then if you selected it out with Person.only("location.address").first.to_json you get
{"_id": "4d22c9781f17fd8150000004", "location": {"_id": "4d22c9781f17fd8230000062", "address": "1234 Fake Street"}}
the _id of the embedded document changed from what it was when it was inserted. This won't happen when just dumping the attributes hash directly rather than calling to_json/to_xml on the them.
The use case I have right now is for API results, building up the criteria and just rendering the results of criteria.all, but I don't want to include the embedded document ids.
Maybe it's not an actual bug or too complicated to fix, but figured it's worth mentioning.