Skip to content

Commit a4f9ac3

Browse files
authored
MONGOID-5547 create index needs to load models (#5526)
* make the load_models task actually load models required decoupling the model loading code from Rails, which required some creative rearchitecting of some brittle tests. * fix other tests that relied on juggling the Rails constant * tidy up the FeatureSandbox * fix typo * parenthesize arguments to `defined?` * extract array to constant for better documentation * update docs * don't try to parse uri string if it hasn't been set
1 parent 4457d3a commit a4f9ac3

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

source/reference/indexes.txt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,42 @@ in Rails console:
143143
# Remove indexes for Model
144144
Model.remove_indexes
145145

146+
Telling Mongoid Where to Look For Models
147+
----------------------------------------
148+
149+
For non-Rails applications, Mongoid's rake tasks will look for models in
150+
``./app/models`` and ``./lib/models``. For Rails, Mongoid will look in
151+
``./app/models`` (or wherever you've configured Rails to look for models). If
152+
your models are in another location, you will need to tell Mongoid where to
153+
look for them with ``Mongoid.model_paths=``. You can do this by setting it
154+
in your application's Rakefile:
155+
156+
.. code-block:: ruby
157+
158+
# Rakefile
159+
160+
# keep the defaults, but add more paths to look for models
161+
Mongoid.model_paths += [ "./src/models", "./lib/documents" ]
162+
163+
# or, override the defaults entirely
164+
Mongoid.model_paths = [ "./src/models", "./lib/documents" ]
165+
166+
Make sure that these paths are in your application's load path, as well. For
167+
example:
168+
169+
.. code-block:: ruby
170+
171+
# Rakefile
172+
173+
$LOAD_PATHS.concat [ "./src/models", "./lib/documents" ]
174+
175+
146176
Using Rake Tasks With Non-Rails Applications
147177
--------------------------------------------
148178

149179
Mongoid's Rake tasks are automatically loaded in Rails applications using
150180
Mongoid. When using Mongoid with a non-Rails application, these tasks must
151-
be loaded manually. This can be achieved by loading them in the Rakefile and
152-
providing an ``:environment`` task to load your application's models:
181+
be loaded manually:
153182

154183
.. code-block:: ruby
155184

@@ -158,10 +187,6 @@ providing an ``:environment`` task to load your application's models:
158187
require 'mongoid'
159188
load 'mongoid/tasks/database.rake'
160189

161-
task :environment do
162-
# Require/load your application's models here
163-
end
164-
165190
If your application uses Bundler, you can require ``bundler/setup`` instead of
166191
explicitly requiring ``mongoid``:
167192

@@ -171,7 +196,3 @@ explicitly requiring ``mongoid``:
171196

172197
require 'bundler/setup'
173198
load 'mongoid/tasks/database.rake'
174-
175-
task :environment do
176-
# Require/load your application's models here
177-
end

0 commit comments

Comments
 (0)