mongoid/mongoid — Issue #1723: Save on embedded_in's grand-parent does not save on first try
Author: ahlwong
Created: 2012-02-15 22:00:23+00:00
State: open

class User
embeds_one :fund, :cascade_callbacks => true
end

class Fund
embeds_many :stock_positions, :cascade_callbacks => true
embedded_in :user

def bug_test
self.stock_positions.new(:stock_id => Stock.where(:symbol => "AAPL").first.id, :pending_action => "buy", :time_of_trade => Time.zone.now, :pending_cash => 1000)
end

end

class StockPosition
embedded_in :fund
end

1.9.2-p0 :018 > u = User.first
1.9.2-p0 :019 > f = u.fund
1.9.2-p0 :020 > f.bug_test
1.9.2-p0 :021 > f.user.save
MONGODB development['users'].update({"_id"=>BSON::ObjectId('4f3b19f96c4be8054e000009')}, {"$set"=>{"updated_at"=>2012-02-15 21:55:34 UTC}})
=> true
1.9.2-p0 :022 > f.user.save
MONGODB development['users'].update({"_id"=>BSON::ObjectId('4f3b19f96c4be8054e000009')}, {"$set"=>{"updated_at"=>2012-02-15 21:55:38 UTC, "fund.updated_at"=>2012-02-15 21:55:38 UTC}})
MONGODB wall_street_game_development['users'].update({"_id"=>BSON::ObjectId('4f3b19f96c4be8054e000009')}, {"$pushAll"=>{"fund.stock_positions"=>[{"shares"=>0, "avg_cost"=>0, "base_daily_avg_cost"=>0, "pending_cash"=>100000, "pending_shares"=>0, "_id"=>BSON::ObjectId('4f3c29d16c4be802e4000002'), "stock_id"=>BSON::ObjectId('4f3b19cc6c4be80621000a59'), "pending_action"=>"buy", "time_of_trade"=>2012-02-15 21:55:29 UTC, "last_checkin"=>2012-02-15 21:55:38 UTC, "updated_at"=>2012-02-15 21:55:34 UTC, "created_at"=>2012-02-15 21:55:34 UTC}]}})

As you can see, calling f.user.save the first time does not save the grand-child but calling it again directly after does save the grand-child. Mongoid 2.3.4 handled this correctly before and would save the grand-child.