Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename model with mongoid

I know with normal migrations I could do rename_table but in mongoid I'm not at all sure how to proceed.

I've got all my models/relations/routes/controllers/etc all renamed but I just need to know how to move the data itself.

This is an embedded embeds_one document btw.

UPDATE:

Looks like simply doing:

Model.all.each {|m| m.rename :old_embedded_association_name, :new_embedded_association_name }

Works. Sound sane?

like image 850
eagspoo Avatar asked Nov 04 '22 20:11

eagspoo


1 Answers

For renaming embedded docs, you treat them just like you would an attribute and rename them using something like this:

ParentModel.all.each {|m| m.rename :old_embedded_association_name, :new_embedded_association_name }

For renaming top level models, you need to access the ruby driver itself and use the #rename_collection method:

Mongoid.database.rename_collection "old_collection_name", "new_collection_name"

To see the old collection name you can do something like this:

Mongoid.database.collections.map {|c| c.name}

like image 50
eagspoo Avatar answered Nov 15 '22 06:11

eagspoo