Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def embeds_many_to_has_many(parent, child)
child_key_name = child.to_s.underscore.pluralize
parent.collection.find({}).each do |parent_document|
next unless parent_document[child_key_name]
parent_document[child_key_name].each do |child_document|
new_child = child_document.merge(
"#{parent.to_s.underscore}_id": parent_document['_id']
)
child.collection.insert_one new_child
end
end
parent.all.unset(child_key_name.to_sym)
end
embeds_many_to_has_many(Post, Comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment