Skip to content

Instantly share code, notes, and snippets.

@draveness
Created August 23, 2017 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draveness/774f7ae2e760e8dcb89a774f8794b129 to your computer and use it in GitHub Desktop.
Save draveness/774f7ae2e760e8dcb89a774f8794b129 to your computer and use it in GitHub Desktop.
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