def [](anchor_or_id)
anchor = if anchor_or_id.is_a?(String)
id, klass = anchor_or_id.split(':')
(klass.constantize || @anchor_reflection.klass).find(id)
else
anchor_or_id
end
result = if @association.loaded?
@association.detect { |x| x.send("#{@anchor_reflection.name}_is?", anchor) }
else
@association.send("#{@anchor_reflection.name}_is", anchor).first
end
result ||= @association.new(@anchor_reflection.name => anchor)
result.origin = self
result.origin_attribute = if @anchor_reflection.options[:polymorphic]
"#{anchor.id}:#{anchor.class.name}"
else
"#{anchor.id}"
end
result
end