|
| 1 | +.. _mongoid-add-to-existing: |
| 2 | + |
| 3 | +====================================== |
| 4 | +Add {+odm+} to an Existing Application |
| 5 | +====================================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: ruby framework, odm, migrate |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to add {+odm+} to an existing Sinatra |
| 24 | +or Ruby on Rails (Rails) application. To learn how to set up a new |
| 25 | +application that uses {+odm+}, see one of the following guides: |
| 26 | + |
| 27 | +- :ref:`mongoid-quick-start-rails` |
| 28 | +- :ref:`mongoid-quick-start-sinatra` |
| 29 | + |
| 30 | +Sinatra Application |
| 31 | +------------------- |
| 32 | + |
| 33 | +To start using {+odm+} in an existing Sinatra application, perform |
| 34 | +the following steps: |
| 35 | + |
| 36 | +1. Add the ``mongoid`` dependency to your application's ``Gemfile``. |
| 37 | + |
| 38 | +#. Create a ``config/mongoid.yml`` configuration file and specify your |
| 39 | + connection target, as shown in the |
| 40 | + :ref:`mongoid-quick-start-sinatra-connect-to-mongodb` step of the |
| 41 | + Quick Start guide. |
| 42 | + |
| 43 | +#. Create an application file and load your configuration file, as shown |
| 44 | + in the :ref:`mongoid-quick-start-sinatra-view-data` step of the Quick |
| 45 | + Start guide. |
| 46 | + |
| 47 | +#. Create {+odm+} models to interact with your data. |
| 48 | + |
| 49 | +Rails Application |
| 50 | +----------------- |
| 51 | + |
| 52 | +You can add {+odm+} to an existing Rails application to run alongside |
| 53 | +other Active Record adapters. To use a combination of adapters, you |
| 54 | +can add the ``mongoid`` dependency and populate the configuration file |
| 55 | +with your connection information to start using MongoDB in your |
| 56 | +application. |
| 57 | + |
| 58 | +To adapt an existing Rails application to use only {+odm+} instead of |
| 59 | +Active Record, you must make other configuration changes, as |
| 60 | +described in the following sections. |
| 61 | + |
| 62 | +Modify Dependencies |
| 63 | +~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +Add the ``mongoid`` gem to your application's ``Gemfile``: |
| 66 | + |
| 67 | +.. code-block:: ruby |
| 68 | + :caption: Gemfile |
| 69 | + |
| 70 | + gem 'mongoid' |
| 71 | + |
| 72 | +To use {+odm+} as the *only* database adapter, remove or comment out any |
| 73 | +RDBMS libraries listed in the ``Gemfile``, such as ``sqlite`` or |
| 74 | +``pg``. |
| 75 | + |
| 76 | +Then, install the dependencies by running the following command: |
| 77 | + |
| 78 | +.. code-block:: sh |
| 79 | + |
| 80 | + bundle install |
| 81 | + |
| 82 | +{+odm+} Configuration |
| 83 | +~~~~~~~~~~~~~~~~~~~~~ |
| 84 | + |
| 85 | +Generate the default {+odm+} configuration by running the following |
| 86 | +command: |
| 87 | + |
| 88 | +.. code-block:: sh |
| 89 | + |
| 90 | + bin/rails g mongoid:config |
| 91 | + |
| 92 | +This generator creates the ``config/mongoid.yml`` configuration file |
| 93 | +used to configure the connection to the MongoDB deployment and the |
| 94 | +``config/initializers/mongoid.rb`` initializer file that you can use |
| 95 | +to set other options. |
| 96 | + |
| 97 | +In the ``config/mongoid.yml`` file, specify your connection string and |
| 98 | +other connection options. |
| 99 | + |
| 100 | +Modify Frameworks |
| 101 | +~~~~~~~~~~~~~~~~~ |
| 102 | + |
| 103 | +Open the ``config/application.rb`` file and examine the contents. If the |
| 104 | +file uses the ``require "rails/all"`` statement to load all Rails components, |
| 105 | +delete this statement. You must add a separate ``require`` statement |
| 106 | +for each Rails component, as shown in the following sample |
| 107 | +``config/application.rb`` file: |
| 108 | + |
| 109 | +.. code-block:: ruby |
| 110 | + :caption: config/application.rb |
| 111 | + |
| 112 | + # Remove or comment out rails/all |
| 113 | + #require "rails/all" |
| 114 | + |
| 115 | + # Add the following instead of rails/all: |
| 116 | + require "rails" |
| 117 | + |
| 118 | + # Comment out unneeded frameworks |
| 119 | + # require "active_record/railtie" rescue LoadError |
| 120 | + # require "active_storage/engine" rescue LoadError |
| 121 | + require "action_controller/railtie" rescue LoadError |
| 122 | + require "action_view/railtie" rescue LoadError |
| 123 | + require "action_mailer/railtie" rescue LoadError |
| 124 | + require "active_job/railtie" rescue LoadError |
| 125 | + require "action_cable/engine" rescue LoadError |
| 126 | + # require "action_mailbox/engine" rescue LoadError |
| 127 | + # require "action_text/engine" rescue LoadError |
| 128 | + require "rails/test_unit/railtie" rescue LoadError |
| 129 | + |
| 130 | +.. note:: |
| 131 | + |
| 132 | + Because they rely on Active Record, the `ActionText |
| 133 | + <https://guides.rubyonrails.org/action_text_overview.html>`__, |
| 134 | + `ActiveStorage <https://edgeguides.rubyonrails.org/active_storage_overview.html>`__, and |
| 135 | + `ActionMailbox |
| 136 | + <https://guides.rubyonrails.org/action_mailbox_basics.html>`__ |
| 137 | + adapters cannot be used alongside {+odm+}. |
| 138 | + |
| 139 | +Disable Active Record Adapters |
| 140 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 141 | + |
| 142 | +In ``config/application.rb`` and your application's other configuration |
| 143 | +files, remove or comment out any references to |
| 144 | +``config.active_record`` and ``config.active_storage``. |
| 145 | + |
| 146 | +Adjust Models |
| 147 | +~~~~~~~~~~~~~ |
| 148 | + |
| 149 | +To migrate from using Active Record to {+odm+}, you must adjust your |
| 150 | +application's existing models. |
| 151 | + |
| 152 | +Active Record models derive from the ``ApplicationRecord`` class and do |
| 153 | +not have column definitions, while {+odm+} models generally have no |
| 154 | +superclass but must include the ``Mongoid::Document`` attribute. |
| 155 | + |
| 156 | +When creating {+odm+} models, you can define fields in the following |
| 157 | +ways: |
| 158 | + |
| 159 | +- Define fields explicitly |
| 160 | +- Use :ref:`dynamic fields <mongoid-dynamic-fields>` |
| 161 | + |
| 162 | +For example, a basic Active Record ``Post`` model might resemble the |
| 163 | +following: |
| 164 | + |
| 165 | +.. code-block:: ruby |
| 166 | + :caption: app/models/post.rb |
| 167 | + |
| 168 | + class Post < ApplicationRecord |
| 169 | + has_many :comments, dependent: :destroy |
| 170 | + end |
| 171 | + |
| 172 | +A similar {+odm+} ``Post`` model might resemble the following: |
| 173 | + |
| 174 | +.. code-block:: ruby |
| 175 | + :caption: app/models/post.rb |
| 176 | + |
| 177 | + class Post |
| 178 | + include Mongoid::Document |
| 179 | + |
| 180 | + field :title, type: String |
| 181 | + field :body, type: String |
| 182 | + |
| 183 | + has_many :comments, dependent: :destroy |
| 184 | + end |
| 185 | + |
| 186 | +Instead of using predefined fields, you can define the ``Post`` model by using |
| 187 | +dynamic fields, as shown in the following code: |
| 188 | + |
| 189 | +.. code-block:: ruby |
| 190 | + :caption: app/models/post.rb |
| 191 | + |
| 192 | + class Post |
| 193 | + include Mongoid::Document |
| 194 | + include Mongoid::Attributes::Dynamic |
| 195 | + |
| 196 | + has_many :comments, dependent: :destroy |
| 197 | + end |
| 198 | + |
| 199 | +Data Migration |
| 200 | +~~~~~~~~~~~~~~ |
| 201 | + |
| 202 | +If you already have data in a relational database that you want to |
| 203 | +move into MongoDB, you must perform a data migration. You don't have to |
| 204 | +perform schema migration because MongoDB does not require |
| 205 | +a predefined schema to store the data. |
| 206 | + |
| 207 | +Migration tools are often specific to datasets. |
| 208 | +Even though {+odm+} supports a superset of Active Record associations, |
| 209 | +model references are stored differently in collections when using |
| 210 | +{+odm+} compared to Active Record. |
| 211 | + |
| 212 | +Visit the following resources to learn more about migrating from an |
| 213 | +RDBMS to MongoDB: |
| 214 | + |
| 215 | +- `RDBMS to MongoDB Migration Guide |
| 216 | + <https://s3.amazonaws.com/info-mongodb-com/RDBMStoMongoDBMigration.pdf>`__ |
| 217 | + in the AWS documentation |
| 218 | + |
| 219 | +- :website:`Modernize your apps with MongoDB Atlas |
| 220 | + </solutions/use-cases/modernize>` on the MongoDB website |
| 221 | + |
| 222 | +Rails API |
| 223 | +~~~~~~~~~ |
| 224 | + |
| 225 | +The process for creating a Rails API application that uses {+odm+} is |
| 226 | +almost the same as for creating a normal application. The only |
| 227 | +difference is that you must add the ``--api`` flag when running ``rails |
| 228 | +new`` to create the application. Migrating a Rails API application to |
| 229 | +{+odm+} follows the same process described in the preceding sections. |
0 commit comments