Given a simple embedded relationship with an extension like this:
class D
include Mongoid::Document
embeds_many :es do
def m
#...
end
end
end
class E
include Mongoid::Document
embedded_in :d
end
You can say things like this:
d = D.find(id)
d.es.m
Inside the extension's m method, how do access the specific d that we're working with?
I'm answering this myself for future reference. If anyone has an official and documented way of doing this, please let me know.
After an hour or so of Googling and reading (and re-reading) the Mongoid documentation, I turned to the Mongoid source code. A bit of searching and guesswork lead me to
@baseand its accessor methodbase:and then you can say this:
baseis documented but the documentation is only there because it is defined usingattr_reader :baseand documentation generated fromattr_readercalls isn't terribly useful.basealso works withhas_manyassociations.How did I figure this out? The documentation on extensions mentions
@targetin an example:@targetisn't what we're looking for,@targetis the array of embedded documents itself but we want what that array is inside of. A bit of grepping about for@targetled me to@base(and the correspondingattr_reader :basecalls) and a quick experiment verified thatbaseis what I was looking for.