-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
Originally I was using association_name for this in beta20, but it looks like it was replaced with metadata.key in RC. It seems like it has a bug when you're using the same class, embedded in one document multiple times. For example the below code:
class Campaign
include Mongoid::Document
embeds_one :email_bid, :class_name => "CampaignBid"
embeds_one :call_bid, :class_name => "CampaignBid"
embeds_one :click_bid, :class_name => "CampaignBid"
end
class CampaignBid
include Mongoid::Document
embedded_in :campaign, :inverse_of => :email_bid
embedded_in :campaign, :inverse_of => :click_bid
embedded_in :campaign, :inverse_of => :call_bid
def email_test
puts "direct: #
#
{self._parent.email_bid.metadata.key}"
puts "self: #
end
def click_test
puts "direct: #{self._parent.click_bid.metadata.object_id} #{self._parent.click_bid.metadata.key}"
puts "self: #{self.metadata.object_id}
#
{self.metadata.key}"
end
end
Call it with
campaign = Campaign.first
c.email_test
c.click_test
You will see
– email_test
direct: 2171600340 email_bid
self: 2171575700 call_bid
– click_test
direct: 2171590080 click_bid
self: 2171575700 call_bid
If I swapped the embedded_in :inverse_of => :call with click, you would see "click_bid" instead of "call_bid" on the two selfs. Basically the bug is that if you call it implicitly, you get the correct metadata, otherwise if you call it with self you get a shared on, regardless of which embedded model it is.