(This is in contrast to the
ActiveRecord and DataMapper method of requiring all models to inherit from a
common base class.) But Rails uses Dependencies to lazy-load models; if we do
not explicitly load them here, they will not exist until their name is referenced.
Replacing Rails Components | 279
(See Chapter 2 for a full explanation of Dependencies.) We load all the Ruby files
in app/models so that ObjectSpace can find them.
5. We start Og, loading its database adapter configuration from the traditional
config/database.yml. The Og manager (an instance of Og::Manager) is stored in
the global variable $og. We do not typically need to use this manager directly; it
finds and injects functionality to (???enchants,??? in Og lingo) our model classes.
The exception is if we need two or more database connections. Each manager is
limited to one database connection, so if we need more connections, we need to
create more managers.
The database connection syntax is similar to ActiveRecord??™s, but there are some differences.
A basic configuration using SQLite3 and a database file of todo_list.db has
the following syntax:
config/database.yml
development:
adapter: sqlite
name: todo_list
Now we are ready to create model classes. Like DataMapper, Og requires us to
define all attributes to be mapped to the database. Also like DataMapper, Og creates
our schema automatically.
Pages:
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425