Class: TransientRecord
- Inherits:
-
Object
- Object
- TransientRecord
- Defined in:
- lib/transient_record.rb
Overview
Transient Record helps define transient tables and Active Record models.
It’s essential to understand Transient Record Contexts in order to use the library effectively. Let’s start the discussion with how Active Record handles connections.
Active Record organizes connection pools around classes. Connecting to multiple databases requires defining multiple abstract classes. For example:
class ApplicationRecord < ActiveRecord::Base
end
class AnalyticsRecord < ApplicationRecord
self.abstract_class = true
connects_to database: { writing: :analytics }
end
In this case, ApplicationRecord.connection returns a connection to the primary database and AnalyticsRecord.connection returns a connection to the other database.
A context is related to an Active Record base class that’s used to access the database directly and to define transient models. After defining an Active Record base class, a context can be created by calling TransientRecord.context_for and must be assigned to a constant.
After the context is created, you can create tables and models by calling Context#create_table and Context#define_model. When you’re done, call TransientRecord.cleanup to drop all transient tables and models
Defined Under Namespace
Modules: Context Classes: Error
Constant Summary collapse
- VERSION =
Transient Record version number.
"2.0.0"
Class Method Summary collapse
- .cleanup ⇒ Object
-
.context_for(base_class) ⇒ Module
Creates a namespace for tables and models corresponding to the given base class.
Class Method Details
.cleanup ⇒ Object
97 98 99 100 |
# File 'lib/transient_record.rb', line 97 def cleanup @contexts.each_value(&:cleanup) nil end |
.context_for(base_class) ⇒ Module
Creates a namespace for tables and models corresponding to the given base class.
Active Record sets up connection pools for abstract Active Record model classes.
93 94 95 |
# File 'lib/transient_record.rb', line 93 def context_for base_class @contexts[base_class] ||= Context.create base_class end |