TheArtistFormerlyKnownAsMongoidDocument
Moving away from Mongo/Mongoid in a Rails app? Still wish to honor legacy
mongo ObjectIds? Well do I have the solution for you!
TheArtistFormerlyKnownAsMongoidDocument is a simple approach that allows
#find methods to still look up by a mongo id.
Installation
Add this line to your application's Gemfile:
gem 'the_artist_formerly_known_as_mongoid_document'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-the_artist_formerly_known_as_mongoid_document
Usage
As you migrate from a Mongoid::Document model to a new ActiveRecord model,
make sure to include an indexed field mongo_id which contains the old mongo
id. In your new model, extend TheArtistFormerlyKnownAsMongoidDocument.
For example:
require 'the_artist_formerly_known_as_mongoid_document'
class Artist < ActiveRecord::Base
extend 'TheArtistFormerlyKnownAsMongoidDocument'
# ...
endYou can then use #find for both mongo_ids:
Artist.find('52829a3e73d8b08b00ad5d63')And of course you can still look for plain-old regular ids:
Artist.find(1)Mongo id and relational database id map
Use #mongo_rdb_id_map to load up a Hash mapping all Mongo ids to
the relational database's ids for a model. This us useful for migrations and
other one-off tasks, but be warned that it can be slow for large records.
Note
If you are using something like FriendlyId, it is wise to put extend TheArtistFormerlyKnownAsMongoidDocument after extend FriendlyId.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request