Skip to content

Commit 5328c20

Browse files
authored
Merge pull request #55 from rustagir/DOCSP-44647-add-to-app
DOCSP-44647: add to existing app
2 parents e894402 + b876e64 commit 5328c20

File tree

10 files changed

+250
-16
lines changed

10 files changed

+250
-16
lines changed

source/add-existing.txt

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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.

source/index.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ MongoDB in Ruby. To work with {+odm+} from the command line using
1414

1515
/quick-start-rails
1616
/quick-start-sinatra
17+
/add-existing
1718
/interact-data
1819
installation-configuration
1920
tutorials
2021
schema-configuration
2122
working-with-data
2223
API <https://mongodb.com/docs/mongoid/master/api/>
23-
release-notes
24-
contributing
25-
additional-resources
26-
ecosystem
24+
/release-notes
25+
/contributing
26+
/additional-resources
27+
/ecosystem

source/quick-start-rails.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _mongoid-quick-start-rails:
22

33
===========================
4-
Quick Start (Ruby on Rails)
4+
Quick Start - Ruby on Rails
55
===========================
66

77
.. facet::
@@ -25,6 +25,9 @@ web application, connect to a MongoDB cluster hosted on MongoDB
2525
Atlas, and perform read and write operations on the data in your
2626
cluster.
2727

28+
To learn how to integrate {+odm+} into an existing application, see the
29+
:ref:`mongoid-add-to-existing` guide.
30+
2831
.. tip::
2932

3033
If you prefer to connect to MongoDB by using the {+ruby-driver+} without
@@ -38,7 +41,7 @@ create flexible data models.
3841
Ruby on Rails is a web application framework for
3942
{+language+}. Rails applications use a model-view-controller (MVC)
4043
architecture that allows you to easily control how your data is
41-
modeled and displayed. {+odm+} replaces the default ``ActiveRecord``
44+
modeled and displayed. {+odm+} replaces the default Active Record
4245
adapter for data modeling in Rails.
4346

4447
To learn more about Ruby on Rails, see the `Getting Started

source/quick-start-rails/download-and-install.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ gems to your web application.
6363
cd {+quickstart-rails-app-name+}
6464

6565
The ``--skip-active-record`` flag instructs Rails to not add
66-
``ActiveRecord`` as a dependency. You don't need this
67-
dependency because you will use {+odm+}
68-
instead.
66+
Active Record as a dependency. You don't need this
67+
dependency because you will use {+odm+} instead.
6968

7069
.. tip:: MacOS Installation Issue
7170

source/quick-start-sinatra.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _mongoid-quick-start-sinatra:
22

33
=====================
4-
Quick Start (Sinatra)
4+
Quick Start - Sinatra
55
=====================
66

77
.. facet::
@@ -24,6 +24,9 @@ This guide shows you how to use {+odm+} in a new Sinatra web application,
2424
connect to a MongoDB cluster hosted on MongoDB Atlas, and perform
2525
read and write operations on the data in your cluster.
2626

27+
To learn how to integrate {+odm+} into an existing application, see the
28+
:ref:`mongoid-add-to-existing` guide.
29+
2730
.. tip::
2831

2932
If you prefer to connect to MongoDB by using the {+ruby-driver+} without

source/reference/compatibility.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ Mongoid is used as a drop-in replacement.
481481
``ActionCable``, however any existing adapter (such as `Redis <https://guides.rubyonrails.org/action_cable_overview.html#redis-adapter>`_)
482482
can be used successfully in conjunction with Mongoid models
483483

484-
.. [#rails-activerecord-dependency] Depends directly on ``ActiveRecord``
484+
.. [#rails-activerecord-dependency] Depends directly on ActiveRecord
485485

486486
.. [#rails-activemodel-dependency] ``Mongoid::Document`` includes ``ActiveModel::Model``
487487
and leverages ``ActiveModel::Validations`` for validations

source/reference/crud.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ each declared field:
707707
# => "Artem"
708708

709709
To use this mechanism, each field must be explicitly declared, or the
710-
model class must enable :ref:`dynamic fields <dynamic-fields>`.
710+
model class must enable :ref:`dynamic fields <mongoid-dynamic-fields>`.
711711

712712

713713
Custom Getters & Setters

source/reference/fields.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,8 +1251,7 @@ Then, use it your model class:
12511251
Note that the handler function will be invoked whenever the option is used
12521252
in the field definition, even if the option's value is false or nil.
12531253

1254-
1255-
.. _dynamic-fields:
1254+
.. _mongoid-dynamic-fields:
12561255

12571256
Dynamic Fields
12581257
==============

source/tutorials/getting-started-rails6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ migrating from ActiveRecord to Mongoid.
481481
ActiveRecord models derive from ``ApplicationRecord`` and do not have
482482
column definitions. Mongoid models generally have no superclass but must
483483
include ``Mongoid::Document``, and usually define the fields explicitly
484-
(but :ref:`dynamic fields <dynamic-fields>` may also be used instead of
484+
(but :ref:`dynamic fields <mongoid-dynamic-fields>` may also be used instead of
485485
explicit field definitions).
486486

487487
For example, a bare-bones Post model may look like this in ActiveRecord:

source/tutorials/getting-started-rails7.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ migrating from ActiveRecord to Mongoid.
400400
ActiveRecord models derive from ``ApplicationRecord`` and do not have
401401
column definitions. Mongoid models generally have no superclass but must
402402
include ``Mongoid::Document``, and usually define the fields explicitly
403-
(but :ref:`dynamic fields <dynamic-fields>` may also be used instead of
403+
(but :ref:`dynamic fields <mongoid-dynamic-fields>` may also be used instead of
404404
explicit field definitions).
405405

406406
For example, a bare-bones Post model may look like this in ActiveRecord:

0 commit comments

Comments
 (0)