Module: ActiveRecord
Overview
Validation error class to wrap association records’ errors, with index_errors support.
Constant Summary
-
UnknownAttributeError =
Raised when unknown attributes are supplied via mass assignment.
ActiveModel::UnknownAttributeError
Class Attribute Summary
-
.db_warnings_action
rw
The action to take when database query produces warning.
- .db_warnings_action=(action) rw
-
.default_timezone=(default_timezone)
writeonly
Determines whether to use
Time.utc
(using:utc
) orTime.local
(using:local
) when pulling dates and times from the database. -
.global_executor_concurrency=(global_executor_concurrency)
rw
Set the .global_executor_concurrency.
- .marshalling_format_version rw
- .marshalling_format_version=(value) rw
-
.permanent_connection_checkout=(value)
writeonly
Defines whether
ActiveRecord::Base.connection
is allowed, deprecated, or entirely disallowed. - .global_executor_concurrency rw Internal use only
Class Method Summary
-
.action_on_strict_loading_violation
Set the application to log or raise when an association violates strict loading.
-
.after_all_transactions_commit(&block)
Registers a block to be called after all the current transactions have been committed.
-
.async_query_executor
Sets the async_query_executor for an application.
-
.db_warnings_ignore
Specify allowlist of database warnings.
-
.disconnect_all!
Explicitly closes all database connections in all pools.
-
.dump_schema_after_migration
Specify whether schema dump should happen at the end of the bin/rails db:migrate command.
-
.dump_schemas
Specifies which database schemas to dump when calling db:schema:dump.
- .eager_load!
-
.error_on_ignored_order
Specifies if an error should be raised if the query has an order being ignored when doing batch queries.
-
.gem_version
Returns the currently loaded version of Active Record as a
Gem::Version
. -
.generate_secure_token_on
Controls when to generate a value for
has_secure_token
declarations. -
.lazily_load_schema_cache
Lazily load the schema cache.
-
.migration_strategy
Specify strategy to use for executing migrations.
-
.protocol_adapters
Provides a mapping between database protocols/DBMSs and the underlying database adapter to be used.
-
.queues
Specifies the names of the queues used by background jobs.
-
.raise_int_wider_than_64bit
Application configurable boolean that denotes whether or not to raise an exception when the PostgreSQLAdapter is provided with an integer that is wider than signed 64bit representation.
-
.schema_cache_ignored_table?(table_name) ⇒ Boolean
Checks to see if the
table_name
is ignored by checking against the .schema_cache_ignored_tables option. -
.schema_cache_ignored_tables
A list of tables or regex’s to match tables to ignore when dumping the schema cache.
-
.schema_format
Specifies the format to use when dumping the database schema with Rails’ Rakefile.
-
.timestamped_migrations
Specify whether or not to use timestamps for migration versions.
-
.use_yaml_unsafe_load
Application configurable boolean that instructs the YAML Coder to use an unsafe load if set to true.
-
.validate_migration_timestamps
Specify whether or not to validate migration timestamps.
-
.verbose_query_logs
Specifies if the methods calling database queries should be logged below their relevant queries.
-
.verify_foreign_keys_for_fixtures
If true,
::Rails
will verify all foreign keys in the database after loading fixtures. -
.version
Returns the currently loaded version of Active Record as a
Gem::Version
. -
.yaml_column_permitted_classes
Application configurable array that provides additional permitted classes to Psych safe_load in the YAML Coder.
- .all_open_transactions Internal use only
- .deprecator Internal use only
- .global_thread_pool_async_query_executor Internal use only
::ActiveSupport::Autoload
- Extended
Instance Attribute Summary
- #action_on_strict_loading_violation rw
- #application_record_class rw
- #async_query_executor rw
- #before_committed_on_all_records rw
- #belongs_to_required_validates_foreign_key rw
- #database_cli rw
- #db_warnings_action readonly
- #db_warnings_ignore rw
- #default_timezone readonly
- #disable_prepared_statements rw
- #dump_schema_after_migration rw
- #dump_schemas rw
- #error_on_ignored_order rw
- #generate_secure_token_on rw
- #index_nested_attribute_errors rw
- #lazily_load_schema_cache rw
- #maintain_test_schema rw
- #migration_strategy rw
- #permanent_connection_checkout readonly
- #protocol_adapters rw
- #query_transformers rw
- #queues rw
- #raise_int_wider_than_64bit rw
- #raise_on_assign_to_attr_readonly rw
- #reading_role rw
- #run_after_transaction_callbacks_in_order_defined rw
- #schema_cache_ignored_tables rw
- #schema_format rw
- #timestamped_migrations rw
- #use_yaml_unsafe_load rw
- #validate_migration_timestamps rw
- #verbose_query_logs rw
- #verify_foreign_keys_for_fixtures rw
- #writing_role rw
- #yaml_column_permitted_classes rw
Class Attribute Details
.db_warnings_action (rw)
The action to take when database query produces warning. Must be one of :ignore
, :log
, :raise
, :report
, or a custom proc. The default is :ignore
.
# File 'activerecord/lib/active_record.rb', line 233
RDoc directive :singleton-method: db_warnings_action
.db_warnings_action=(action) (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 235
def self.db_warnings_action=(action) @db_warnings_action = case action when :ignore nil when :log ->(warning) do = "[#{warning.class}] #{warning.}" += " (#{warning.code})" if warning.code ActiveRecord::Base.logger.warn( ) end when :raise ->(warning) { raise warning } when :report ->(warning) { Rails.error.report(warning, handled: true) } when Proc action else raise ArgumentError, "db_warnings_action must be one of :ignore, :log, :raise, :report, or a custom proc." end end
.default_timezone=(default_timezone) (writeonly)
Determines whether to use Time.utc
(using :utc
) or Time.local
(using :local
) when pulling dates and times from the database. This is set to :utc
by default.
# File 'activerecord/lib/active_record.rb', line 218
def self.default_timezone=(default_timezone) unless %i(local utc).include?(default_timezone) raise ArgumentError, "default_timezone must be either :utc (default) or :local." end @default_timezone = default_timezone end
.global_executor_concurrency (rw)
# File 'activerecord/lib/active_record.rb', line 306
def self.global_executor_concurrency # :nodoc: @global_executor_concurrency ||= nil end
.global_executor_concurrency=(global_executor_concurrency) (rw)
Set the .global_executor_concurrency. This configuration value can only be used with the global thread pool async query executor.
# File 'activerecord/lib/active_record.rb', line 298
def self.global_executor_concurrency=(global_executor_concurrency) if self.async_query_executor.nil? || self.async_query_executor == :multi_thread_pool raise ArgumentError, "`global_executor_concurrency` cannot be set when the executor is nil or set to `:multi_thread_pool`. For multiple thread pools, please set the concurrency in your database configuration." end @global_executor_concurrency = global_executor_concurrency end
.marshalling_format_version (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 463
def self.marshalling_format_version Marshalling.format_version end
.marshalling_format_version=(value) (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 467
def self.marshalling_format_version=(value) Marshalling.format_version = value end
.permanent_connection_checkout=(value) (writeonly)
Defines whether ActiveRecord::Base.connection
is allowed, deprecated, or entirely disallowed.
# File 'activerecord/lib/active_record.rb', line 314
def self.permanent_connection_checkout=(value) unless [true, :deprecated, :disallowed].include?(value) raise ArgumentError, "permanent_connection_checkout must be one of: `true`, `:deprecated` or `:disallowed`" end @permanent_connection_checkout = value end
Class Method Details
.action_on_strict_loading_violation
Set the application to log or raise when an association violates strict loading. Defaults to :raise
.
# File 'activerecord/lib/active_record.rb', line 361
RDoc directive :singleton-method: action_on_strict_loading_violation
.after_all_transactions_commit(&block)
Registers a block to be called after all the current transactions have been committed.
If there is no currently open transaction, the block is called immediately.
If there are multiple nested transactions, the block is called after the outermost one has been committed,
If any of the currently open transactions is rolled back, the block is never called.
If multiple transactions are open across multiple databases, the block will be invoked if and once all of them have been committed. But note that nesting transactions across two distinct databases is a sharding anti-pattern that comes with a world of hurts.
# File 'activerecord/lib/active_record.rb', line 527
def self.after_all_transactions_commit(&block) open_transactions = all_open_transactions if open_transactions.empty? yield elsif open_transactions.size == 1 open_transactions.first.after_commit(&block) else count = open_transactions.size callback = -> do count -= 1 block.call if count.zero? end open_transactions.each do |t| t.after_commit(&callback) end open_transactions = nil # rubocop:disable Lint/UselessAssignment avoid holding it in the closure end end
.all_open_transactions
# File 'activerecord/lib/active_record.rb', line 547
def self.all_open_transactions # :nodoc: open_transactions = [] Base.connection_handler.each_connection_pool do |pool| if active_connection = pool.active_connection if active_connection.current_transaction.open? && active_connection.current_transaction.joinable? open_transactions << active_connection.current_transaction end end end open_transactions end
.async_query_executor
Sets the async_query_executor for an application. By default the thread pool executor set to nil
which will not run queries in the background. Applications must configure a thread pool executor to use this feature. Options are:
* nil - Does not initialize a thread pool executor. Any async calls will be
run in the foreground.
* :global_thread_pool - Initializes a single {Concurrent::ThreadPoolExecutor}
that uses the {async_query_concurrency} for the {max_threads} value.
* :multi_thread_pool - Initializes a {Concurrent::ThreadPoolExecutor} for each
database connection. The initializer values are defined in the configuration hash.
# File 'activerecord/lib/active_record.rb', line 283
RDoc directive :singleton-method: async_query_executor
.db_warnings_ignore
Specify allowlist of database warnings.
# File 'activerecord/lib/active_record.rb', line 262
RDoc directive :singleton-method: db_warnings_ignore
.deprecator
# File 'activerecord/lib/active_record/deprecator.rb', line 4
def self.deprecator # :nodoc: @deprecator ||= ActiveSupport::Deprecation.new end
.disconnect_all!
Explicitly closes all database connections in all pools.
# File 'activerecord/lib/active_record.rb', line 510
def self.disconnect_all! ConnectionAdapters::PoolConfig.disconnect_all! end
.dump_schema_after_migration
Specify whether schema dump should happen at the end of the bin/rails db:migrate command. This is true by default, which is useful for the development environment. This should ideally be false in the production environment where dumping schema is rarely needed.
# File 'activerecord/lib/active_record.rb', line 409
RDoc directive :singleton-method: dump_schema_after_migration
.dump_schemas
Specifies which database schemas to dump when calling db:schema:dump. If the value is :schema_search_path
(the default), any schemas listed in schema_search_path are dumped. Use :all
to dump all schemas regardless of schema_search_path, or a string of comma separated schemas for a custom list.
# File 'activerecord/lib/active_record.rb', line 419
RDoc directive :singleton-method: dump_schemas
.eager_load!
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 499
def self.eager_load! super ActiveRecord::Locking.eager_load! ActiveRecord::Scoping.eager_load! ActiveRecord::Associations.eager_load! ActiveRecord::AttributeMethods.eager_load! ActiveRecord::ConnectionAdapters.eager_load! ActiveRecord::Encryption.eager_load! end
.error_on_ignored_order
Specifies if an error should be raised if the query has an order being ignored when doing batch queries. Useful in applications where the scope being ignored is error-worthy, rather than a warning.
# File 'activerecord/lib/active_record.rb', line 380
RDoc directive :singleton-method: error_on_ignored_order
.gem_version
Returns the currently loaded version of Active Record as a Gem::Version
.
.generate_secure_token_on
Controls when to generate a value for has_secure_token
declarations. Defaults to :create
.
# File 'activerecord/lib/active_record.rb', line 460
RDoc directive :singleton-method: generate_secure_token_on
.global_thread_pool_async_query_executor
# File 'activerecord/lib/active_record.rb', line 286
def self.global_thread_pool_async_query_executor # :nodoc: concurrency = global_executor_concurrency || 4 @global_thread_pool_async_query_executor ||= Concurrent::ThreadPoolExecutor.new( min_threads: 0, max_threads: concurrency, max_queue: concurrency * 4, fallback_policy: :caller_runs ) end
.lazily_load_schema_cache
Lazily load the schema cache. This option will load the schema cache when a connection is established rather than on boot.
# File 'activerecord/lib/active_record.rb', line 189
RDoc directive :singleton-method: lazily_load_schema_cache
.migration_strategy
Specify strategy to use for executing migrations.
# File 'activerecord/lib/active_record.rb', line 400
RDoc directive :singleton-method: migration_strategy
.protocol_adapters
Provides a mapping between database protocols/DBMSs and the underlying database adapter to be used. This is used only by the DATABASE_URL
environment variable.
Example
DATABASE_URL="mysql://myuser:mypass@localhost/somedatabase"
The above URL specifies that MySQL is the desired protocol/DBMS, and the application configuration can then decide which adapter to use. For this example the default mapping is from mysql
to mysql2
, but :trilogy
is also supported.
ActiveRecord.protocol_adapters.mysql = "mysql2"
The protocols names are arbitrary, and external database adapters can be registered and set here.
# File 'activerecord/lib/active_record.rb', line 490
RDoc directive :singleton-method: protocol_adapters
.queues
Specifies the names of the queues used by background jobs.
# File 'activerecord/lib/active_record.rb', line 336
RDoc directive :singleton-method: queues
.raise_int_wider_than_64bit
Application configurable boolean that denotes whether or not to raise an exception when the PostgreSQLAdapter is provided with an integer that is wider than signed 64bit representation
# File 'activerecord/lib/active_record.rb', line 446
RDoc directive :singleton-method: raise_int_wider_than_64bit
.schema_cache_ignored_table?(table_name) ⇒ Boolean
Checks to see if the table_name
is ignored by checking against the .schema_cache_ignored_tables option.
ActiveRecord.schema_cache_ignored_table?(:developers)
# File 'activerecord/lib/active_record.rb', line 205
def self.schema_cache_ignored_table?(table_name) ActiveRecord.schema_cache_ignored_tables.any? do |ignored| ignored === table_name end end
.schema_cache_ignored_tables
A list of tables or regex’s to match tables to ignore when dumping the schema cache. For example if this is set to [/^_/]
the schema cache will not dump tables named with an underscore.
# File 'activerecord/lib/active_record.rb', line 197
RDoc directive :singleton-method: schema_cache_ignored_tables
.schema_format
Specifies the format to use when dumping the database schema with Rails’ Rakefile. If :sql
, the schema is dumped as (potentially database- specific) SQL statements. If :ruby
, the schema is dumped as an ::ActiveRecord::Schema
file which can be loaded into any database that supports migrations. Use :ruby
if you want to have different database adapters for, e.g., your development and test environments.
# File 'activerecord/lib/active_record.rb', line 372
RDoc directive :singleton-method: schema_format
.timestamped_migrations
Specify whether or not to use timestamps for migration versions
# File 'activerecord/lib/active_record.rb', line 386
RDoc directive :singleton-method:
.use_yaml_unsafe_load
Application configurable boolean that instructs the YAML Coder to use an unsafe load if set to true.
# File 'activerecord/lib/active_record.rb', line 438
RDoc directive :singleton-method: use_yaml_unsafe_load
.validate_migration_timestamps
Specify whether or not to validate migration timestamps. When set, an error will be raised if a timestamp is more than a day ahead of the timestamp associated with the current time. .timestamped_migrations must be set to true.
# File 'activerecord/lib/active_record.rb', line 394
RDoc directive :singleton-method:
.verbose_query_logs
Specifies if the methods calling database queries should be logged below their relevant queries. Defaults to false.
# File 'activerecord/lib/active_record.rb', line 329
RDoc directive :singleton-method: verbose_query_logs
.verify_foreign_keys_for_fixtures
If true, ::Rails
will verify all foreign keys in the database after loading fixtures. An error will be raised if there are any foreign key violations, indicating incorrectly written fixtures. Supported by PostgreSQL and SQLite.
# File 'activerecord/lib/active_record.rb', line 428
RDoc directive :singleton-method: verify_foreign_keys_for_fixtures
.version
Returns the currently loaded version of Active Record as a Gem::Version
.
# File 'activerecord/lib/active_record/version.rb', line 7
def self.version gem_version end
.yaml_column_permitted_classes
Application configurable array that provides additional permitted classes to Psych safe_load in the YAML Coder
# File 'activerecord/lib/active_record.rb', line 453
RDoc directive :singleton-method: yaml_column_permitted_classes
Instance Attribute Details
#action_on_strict_loading_violation (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 361
singleton_class.attr_accessor :action_on_strict_loading_violation
#application_record_class (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 354
singleton_class.attr_accessor :application_record_class
#async_query_executor (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 283
singleton_class.attr_accessor :async_query_executor
#before_committed_on_all_records (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 348
singleton_class.attr_accessor :before_committed_on_all_records
#belongs_to_required_validates_foreign_key (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 345
singleton_class.attr_accessor :belongs_to_required_validates_foreign_key
#database_cli (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 211
singleton_class.attr_accessor :database_cli
#db_warnings_action (readonly)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 233
singleton_class.attr_reader :db_warnings_action
#db_warnings_ignore (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 262
singleton_class.attr_accessor :db_warnings_ignore
#default_timezone (readonly)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 214
singleton_class.attr_reader :default_timezone
#disable_prepared_statements (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 182
singleton_class.attr_accessor :disable_prepared_statements
#dump_schema_after_migration (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 409
singleton_class.attr_accessor :dump_schema_after_migration
#dump_schemas (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 419
singleton_class.attr_accessor :dump_schemas
#error_on_ignored_order (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 380
singleton_class.attr_accessor :error_on_ignored_order
#generate_secure_token_on (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 460
singleton_class.attr_accessor :generate_secure_token_on
#index_nested_attribute_errors (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 321
singleton_class.attr_accessor :index_nested_attribute_errors
#lazily_load_schema_cache (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 189
singleton_class.attr_accessor :lazily_load_schema_cache
#maintain_test_schema (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 339
singleton_class.attr_accessor :maintain_test_schema
#migration_strategy (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 400
singleton_class.attr_accessor :migration_strategy
#permanent_connection_checkout (readonly)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 311
singleton_class.attr_reader :permanent_connection_checkout
#protocol_adapters (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 490
singleton_class.attr_accessor :protocol_adapters
#query_transformers (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 431
singleton_class.attr_accessor :query_transformers
#queues (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 336
singleton_class.attr_accessor :queues
#raise_int_wider_than_64bit (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 446
singleton_class.attr_accessor :raise_int_wider_than_64bit
#raise_on_assign_to_attr_readonly (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 342
singleton_class.attr_accessor :raise_on_assign_to_attr_readonly
#reading_role (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 268
singleton_class.attr_accessor :reading_role
#run_after_transaction_callbacks_in_order_defined (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 351
singleton_class.attr_accessor :run_after_transaction_callbacks_in_order_defined
#schema_cache_ignored_tables (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 197
singleton_class.attr_accessor :schema_cache_ignored_tables
#schema_format (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 372
singleton_class.attr_accessor :schema_format
#timestamped_migrations (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 386
singleton_class.attr_accessor :
#use_yaml_unsafe_load (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 438
singleton_class.attr_accessor :use_yaml_unsafe_load
#validate_migration_timestamps (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 394
singleton_class.attr_accessor :
#verbose_query_logs (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 329
singleton_class.attr_accessor :verbose_query_logs
#verify_foreign_keys_for_fixtures (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 428
singleton_class.attr_accessor :verify_foreign_keys_for_fixtures
#writing_role (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 265
singleton_class.attr_accessor :writing_role
#yaml_column_permitted_classes (rw)
[ GitHub ]# File 'activerecord/lib/active_record.rb', line 453
singleton_class.attr_accessor :yaml_column_permitted_classes