Created
August 23, 2017 23:01
-
-
Save draveness/774f7ae2e760e8dcb89a774f8794b129 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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