Update dependency rails to '~> 7.2.0'
This MR contains the following updates:
| Package | Update | Change |
|---|---|---|
| rails (source, changelog) | minor |
'~> 7.0.8.4' -> '~> 7.2.0'
|
| rails (source, changelog) | minor |
'~> 7.1.3.4' -> '~> 7.2.0'
|
MR created with the help of gitlab-org/frontend/renovate-gitlab-bot
Release Notes
rails/rails (rails)
v7.2.0: 7.2.0
Active Support
-
Fix
delegate_missing_to allow_nil: truewhen called with implict selfclass Person delegate_missing_to :address, allow_nil: true def address nil end def berliner? city == "Berlin" end end Person.new.city # => nil Person.new.berliner? # undefined local variable or method `city' for an instance of Person (NameError)Jean Boussier
-
Add
loggeras a dependency since it is a bundled gem candidate for Ruby 3.5Earlopain
-
Define
Digest::UUID.nil_uuid, which returns the so-called nil UUID.Xavier Noria
-
Support
durationtype inActiveSupport::XmlMini.heka1024
-
Remove deprecated
ActiveSupport::Notifications::Event#childrenandActiveSupport::Notifications::Event#parent_of?.Rafael Mendonça França
-
Remove deprecated support to call the following methods without passing a deprecator:
deprecatedeprecate_constantActiveSupport::Deprecation::DeprecatedObjectProxy.newActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.newActiveSupport::Deprecation::DeprecatedConstantProxy.newassert_deprecatedassert_not_deprecatedcollect_deprecations
Rafael Mendonça França
-
Remove deprecated
ActiveSupport::Deprecationdelegation to instance.Rafael Mendonça França
-
Remove deprecated
SafeBuffer#clone_empty.Rafael Mendonça França
-
Remove deprecated
#to_default_sfromArray,Date,DateTimeandTime.Rafael Mendonça França
-
Remove deprecated support to passing
Dalli::Clientinstances toMemCacheStore.Rafael Mendonça França
-
Remove deprecated
config.active_support.use_rfc4122_namespaced_uuids.Rafael Mendonça França
-
Remove deprecated
config.active_support.remove_deprecated_time_with_zone_name.Rafael Mendonça França
-
Remove deprecated
config.active_support.disable_to_s_conversion.Rafael Mendonça França
-
Remove deprecated support to bolding log text with positional boolean in
ActiveSupport::LogSubscriber#color.Rafael Mendonça França
-
Remove deprecated constants
ActiveSupport::LogSubscriber::CLEARandActiveSupport::LogSubscriber::BOLD.Rafael Mendonça França
-
Remove deprecated support for
config.active_support.cache_format_version = 6.1.Rafael Mendonça França
-
Remove deprecated
:pool_sizeand:pool_timeoutoptions for the cache storage.Rafael Mendonça França
-
Warn on tests without assertions.
ActiveSupport::TestCasenow warns when tests do not run any assertions. This is helpful in detecting broken tests that do not perform intended assertions.fatkodima
-
Support
hexBinarytype inActiveSupport::XmlMini.heka1024
-
Deprecate
ActiveSupport::ProxyObjectin favor of Ruby's built-inBasicObject.Earlopain
-
stub_constnow accepts aexists: falseparameter to allow stubbing missing constants.Jean Boussier
-
Make
ActiveSupport::BacktraceCleanercopy filters and silencers on dup and clone.Previously the copy would still share the internal silencers and filters array, causing state to leak.
Jean Boussier
-
Updating Astana with Western Kazakhstan TZInfo identifier.
Damian Nelson
-
Add filename support for
ActiveSupport::Logger.logger_outputs_to?.logger = Logger.new('/var/log/rails.log') ActiveSupport::Logger.logger_outputs_to?(logger, '/var/log/rails.log')Christian Schmidt
-
Include
IPAddr#prefixwhen serializing anIPAddrusing theActiveSupport::MessagePackserializer.This change is backward and forward compatible — old payloads can still be read, and new payloads will be readable by older versions of Rails.
Taiki Komaba
-
Add
default:support forActiveSupport::CurrentAttributes.attribute.class Current < ActiveSupport::CurrentAttributes attribute :counter, default: 0 endSean Doyle
-
Yield instance to
Object#withblock.client.with(timeout: 5_000) do |c| c.get("/commits") endSean Doyle
-
Use logical core count instead of physical core count to determine the default number of workers when parallelizing tests.
Jonathan Hefner
-
Fix
Time.now/DateTime.now/Date.todayto return results in a system timezone after#travel_to.There is a bug in the current implementation of #travel_to: it remembers a timezone of its argument, and all stubbed methods start returning results in that remembered timezone. However, the expected behavior is to return results in a system timezone.
Aleksei Chernenkov
-
Add
ErrorReported#unexpectedto report precondition violations.For example:
def edit if published? Rails.error.unexpected("[BUG] Attempting to edit a published article, that shouldn't be possible") return false end
...
end
```
The above will raise an error in development and test, but only report the error in production.
*Jean Boussier*
-
Make the order of read_multi and write_multi notifications for
Cache::Store#fetch_multioperations match the order they are executed in.Adam Renberg Tamm
-
Make return values of
Cache::Store#writeconsistent.The return value was not specified before. Now it returns
trueon a successful write,nilif there was an error talking to the cache backend, andfalseif the write failed for another reason (e.g. the key already exists andunless_exist: truewas passed).Sander Verdonschot
-
Fix logged cache keys not always matching actual key used by cache action.
Hartley McGuire
-
Improve error messages of
assert_changesandassert_no_changes.assert_changeserror messages now display objects with.inspectto make it easier to differentiate nil from empty strings, strings from symbols, etc.assert_no_changeserror messages now surface the actual value.pcreux
-
Fix
#to_fs(:human_size)to correctly work with negative numbers.Earlopain
-
Fix
BroadcastLogger#dupso that it duplicates the logger'sbroadcasts.Andrew Novoselac
-
Fix issue where
bootstrap.rboverwrites thelevelof aBroadcastLogger'sbroadcasts.Andrew Novoselac
-
Fix compatibility with the
semantic_loggergem.The
semantic_loggergem doesn't behave exactly like stdlib logger in thatSemanticLogger#levelreturns a Symbol while stdlibLogger#levelreturns an Integer.This caused the various
LogSubscriberclasses in Rails to break when assigned aSemanticLoggerinstance.Jean Boussier, ojab
-
Fix MemoryStore to prevent race conditions when incrementing or decrementing.
Pierre Jambet
-
Implement
HashWithIndifferentAccess#to_proc.Previously, calling
#to_proconHashWithIndifferentAccessobject used inherited#to_procmethod from theHashclass, which was not able to access values using indifferent keys.fatkodima
Active Model
-
Fix a bug where type casting of string to
TimeandDateTimedoesn't calculate minus minute value in TZ offset correctly.Akira Matsuda
-
Port the
type_for_attributemethod to Active Model. Classes that includeActiveModel::Attributeswill now provide this method. This method behaves the same for Active Model as it does for Active Record.class MyModel include ActiveModel::Attributes attribute :my_attribute, :integer end MyModel.type_for_attribute(:my_attribute) # => #<ActiveModel::Type::Integer ...>Jonathan Hefner
Active Record
-
Handle commas in Sqlite3 default function definitions.
Stephen Margheim
-
Fixes
validates_associatedraising an exception when configured with a singular association and havingindex_nested_attribute_errorsenabled.Martin Spickermann
-
The constant
ActiveRecord::ImmutableRelationhas been deprecated because we want to reserve that name for a stronger sense of "immutable relation". Please useActiveRecord::UnmodifiableRelationinstead.Xavier Noria
-
Add condensed
#inspectforConnectionPool,AbstractAdapter, andDatabaseConfig.Hartley McGuire
-
Fixed a memory performance issue in Active Record attribute methods definition.
Jean Boussier
-
Define the new Active Support notification event
start_transaction.active_record.This event is fired when database transactions or savepoints start, and complements
transaction.active_record, which is emitted when they finish.The payload has the transaction (
:transaction) and the connection (:connection).Xavier Noria
-
Fix an issue where the IDs reader method did not return expected results for preloaded associations in models using composite primary keys.
Jay Ang
-
The payload of
sql.active_recordActive Support notifications now has the current transaction in the:transactionkey.Xavier Noria
-
The payload of
transaction.active_recordActive Support notifications now has the transaction the event is related to in the:transactionkey.Xavier Noria
-
Define
ActiveRecord::Transaction#uuid, which returns a UUID for the database transaction. This may be helpful when tracing database activity. These UUIDs are generated only on demand.Xavier Noria
-
Fix inference of association model on nested models with the same demodularized name.
E.g. with the following setup:
class Nested::Post < ApplicationRecord has_one :post, through: :other endBefore,
#postwould infer the model asNested::Post, but now it correctly infersPost.Joshua Young
-
PostgreSQL
Cidr#change?detects the address prefix change.Taketo Takashima
-
Change
BatchEnumerator#destroy_allto return the total number of affected rows.Previously, it always returned
nil.fatkodima
-
Support
touch_allin batches.Post.in_batches.touch_allfatkodima
-
Add support for
:if_not_existsand:forceoptions tocreate_schema.fatkodima
-
Fix
index_errorshaving incorrect index in association validation errors.lulalala
-
Add
index_errors: :nested_attributes_ordermode.This indexes the association validation errors based on the order received by nested attributes setter, and respects the
reject_ifconfiguration. This enables API to provide enough information to the frontend to map the validation errors back to their respective form fields.lulalala
-
Add
Rails.application.config.active_record.postgresql_adapter_decode_datesto opt out of decoding dates automatically with the postgresql adapter. Defaults to true.Joé Dupuis
-
Association option
query_constraintsis deprecated in favor offoreign_key.Nikita Vasilevsky
-
Add
ENV["SKIP_TEST_DATABASE_TRUNCATE"]flag to speed up multi-process test runs on large DBs when all tests run within default transaction.This cuts ~10s from the test run of HEY when run by 24 processes against the 178 tables, since ~4,000 table truncates can then be skipped.
DHH
-
Added support for recursive common table expressions.
Post.with_recursive( post_and_replies: [ Post.where(id: 42), Post.joins('JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id'), ] )Generates the following SQL:
WITH RECURSIVE "post_and_replies" AS ( (SELECT "posts".* FROM "posts" WHERE "posts"."id" = 42) UNION ALL (SELECT "posts".* FROM "posts" JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id) ) SELECT "posts".* FROM "posts"ClearlyClaire
-
validate_constraintcan be called in achange_tableblock.ex:
change_table :products do |t| t.check_constraint "price > discounted_price", name: "price_check", validate: false t.validate_check_constraint "price_check" endCody Cutrer
-
PostgreSQLAdapternow decodes columns of type date toDateinstead of string.Ex:
ActiveRecord::Base.connection .select_value("select '2024-01-01'::date").class #=> DateJoé Dupuis
-
Strict loading using
:n_plus_one_onlydoes not eagerly load child associations.With this change, child associations are no longer eagerly loaded, to match intended behavior and to prevent non-deterministic order issues caused by calling methods like
firstorlast. Asfirstandlastdon't cause an N+1 by themselves, calling child associations will no longer raise. Fixes #49473.Before:
person = Person.find(1) person.strict_loading!(mode: :n_plus_one_only) person.posts.first
SELECT * FROM posts WHERE person_id = 1; -- non-deterministic order
person.posts.first.firm # raises ActiveRecord::StrictLoadingViolationError
```
After:
```ruby
person = Person.find(1)
person.strict_loading!(mode: :n_plus_one_only)
person.posts.first # this is 1+1, not N+1
SELECT * FROM posts WHERE person_id = 1 ORDER BY id LIMIT 1;
person.posts.first.firm # no longer raises
```
*Reid Lynch*
-
Allow
Sqlite3Adapterto usesqlite3gem version2.x.Mike Dalessio
-
Allow
ActiveRecord::Base#pluckto accept hash values.
Before
Post.joins(:comments).pluck("posts.id", "comments.id", "comments.body")
After
Post.joins(:comments).pluck(posts: [:id], comments: [:id, :body])
```
*fatkodima*
-
Raise an
ActiveRecord::ActiveRecordErrorerror when the MySQL database returns an invalid version string.Kevin McPhillips
-
ActiveRecord::Base.transactionnow yields anActiveRecord::Transactionobject.This allows to register callbacks on it.
Article.transaction do |transaction| article.update(published: true) transaction.after_commit do PublishNotificationMailer.with(article: article).deliver_later end endJean Boussier
-
Add
ActiveRecord::Base.current_transaction.Returns the current transaction, to allow registering callbacks on it.
Article.current_transaction.after_commit do PublishNotificationMailer.with(article: article).deliver_later endJean Boussier
-
Add
ActiveRecord.after_all_transactions_commitcallback.Useful for code that may run either inside or outside a transaction and needs to perform work after the state changes have been properly persisted.
def publish_article(article) article.update(published: true) ActiveRecord.after_all_transactions_commit do PublishNotificationMailer.with(article: article).deliver_later end endIn the above example, the block is either executed immediately if called outside of a transaction, or called after the open transaction is committed.
If the transaction is rolled back, the block isn't called.
Jean Boussier
-
Add the ability to ignore counter cache columns until they are backfilled.
Starting to use counter caches on existing large tables can be troublesome, because the column values must be backfilled separately of the column addition (to not lock the table for too long) and before the use of
:counter_cache(otherwise methods likesize/any?/etc, which use counter caches internally, can produce incorrect results). People usually use database triggers or callbacks on child associations while backfilling before introducing a counter cache configuration to the association.Now, to safely backfill the column, while keeping the column updated with child records added/removed, use:
class Comment < ApplicationRecord belongs_to :post, counter_cache: { active: false } endWhile the counter cache is not "active", the methods like
size/any?/etc will not use it, but get the results directly from the database. After the counter cache column is backfilled, simply remove the{ active: false }part from the counter cache definition, and it will now be used by the mentioned methods.fatkodima
-
Retry known idempotent SELECT queries on connection-related exceptions.
SELECT queries we construct by walking the Arel tree and / or with known model attributes are idempotent and can safely be retried in the case of a connection error. Previously, adapters such as
TrilogyAdapterwould raiseActiveRecord::ConnectionFailed: Trilogy::EOFErrorwhen encountering a connection error mid-request.Adrianna Chang
-
Allow association's
foreign_keyto be composite.query_constraintsoption was the only way to configure a composite foreign key by passing anArray. Now it's possible to pass an Array value asforeign_keyto achieve the same behavior of an association.Nikita Vasilevsky
-
Allow association's
primary_keyto be composite.Association's
primary_keycan be composite when derived from associated modelprimary_keyorquery_constraints. Now it's possible to explicitly set it as composite on the association.Nikita Vasilevsky
-
Add
config.active_record.permanent_connection_checkoutsetting.Controls whether
ActiveRecord::Base.connectionraises an error, emits a deprecation warning, or neither.ActiveRecord::Base.connectioncheckouts a database connection from the pool and keeps it leased until the end of the request or job. This behavior can be undesirable in environments that use many more threads or fibers than there is available connections.This configuration can be used to track down and eliminate code that calls
ActiveRecord::Base.connectionand migrate it to useActiveRecord::Base.with_connectioninstead.The default behavior remains unchanged, and there is currently no plans to change the default.
Jean Boussier
-
Add dirties option to uncached.
This adds a
dirtiesoption toActiveRecord::Base.uncachedandActiveRecord::ConnectionAdapters::ConnectionPool#uncached.When set to
true(the default), writes will clear all query caches belonging to the current thread. When set tofalse, writes to the affected connection pool will not clear any query cache.This is needed by Solid Cache so that cache writes do not clear query caches.
Donal McBreen
-
Deprecate
ActiveRecord::Base.connectionin favor of.lease_connection.The method has been renamed as
lease_connectionto better reflect that the returned connection will be held for the duration of the request or job.This deprecation is a soft deprecation, no warnings will be issued and there is no current plan to remove the method.
Jean Boussier
-
Deprecate
ActiveRecord::ConnectionAdapters::ConnectionPool#connection.The method has been renamed as
lease_connectionto better reflect that the returned connection will be held for the duration of the request or job.Jean Boussier
-
Expose a generic fixture accessor for fixture names that may conflict with Minitest.
assert_equal "Ruby on Rails", web_sites(:rubyonrails).name assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).nameJean Boussier
-
Using
Model.query_constraintswith a single non-primary-key column used to raise as expected, but with an incorrect error message.This has been fixed to raise with a more appropriate error message.
Joshua Young
-
Fix
has_oneassociation autosave setting the foreign key attribute when it is unchanged.This behavior is also inconsistent with autosaving
belongs_toand can have unintended side effects like raising anActiveRecord::ReadonlyAttributeErrorwhen the foreign key attribute is marked as read-only.Joshua Young
-
Remove deprecated behavior that would rollback a transaction block when exited using
return,breakorthrow.Rafael Mendonça França
-
Deprecate
Rails.application.config.active_record.commit_transaction_on_non_local_return.Rafael Mendonça França
-
Remove deprecated support to pass
rewheretoActiveRecord::Relation#merge.Rafael Mendonça França
-
Remove deprecated support to pass
deferrable: truetoadd_foreign_key.Rafael Mendonça França
-
Remove deprecated support to quote
ActiveSupport::Duration.Rafael Mendonça França
-
Remove deprecated
#quote_bound_value.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::ConnectionAdapters::ConnectionPool#connection_klass.Rafael Mendonça França
-
Remove deprecated support to apply
#connection_pool_list,#active_connections?,#clear_active_connections!,#clear_reloadable_connections!,#clear_all_connections!and#flush_idle_connections!to the connections pools for the current role when theroleargument isn't provided.Rafael Mendonça França
-
Remove deprecated
#all_connection_pools.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::ConnectionAdapters::SchemaCache#data_sources.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::ConnectionAdapters::SchemaCache.load_from.Rafael Mendonça França
-
Remove deprecated
#all_foreign_keys_valid?from database adapters.Rafael Mendonça França
-
Remove deprecated support to passing coder and class as second argument to
serialize.Rafael Mendonça França
-
Remove deprecated support to
ActiveRecord::Base#read_attribute(:id)to return the custom primary key value.Rafael Mendonça França
-
Remove deprecated
TestFixtures.fixture_path.Rafael Mendonça França
-
Remove deprecated behavior to support referring to a singular association by its plural name.
Rafael Mendonça França
-
Deprecate
Rails.application.config.active_record.allow_deprecated_singular_associations_name.Rafael Mendonça França
-
Remove deprecated support to passing
SchemaMigrationandInternalMetadataclasses as arguments toActiveRecord::MigrationContext.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Migration.check_pending!method.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::LogSubscriber.runtimemethod.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::LogSubscriber.runtime=method.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::LogSubscriber.reset_runtimemethod.Rafael Mendonça França
-
Remove deprecated support to define
explainin the connection adapter with 2 arguments.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::ActiveJobRequiredError.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Base.clear_active_connections!.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Base.clear_reloadable_connections!.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Base.clear_all_connections!.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Base.flush_idle_connections!.Rafael Mendonça França
-
Remove deprecated
nameargument fromActiveRecord::Base.remove_connection.Rafael Mendonça França
-
Remove deprecated support to call
alias_attributewith non-existent attribute names.Rafael Mendonça França
-
Remove deprecated
Rails.application.config.active_record.suppress_multiple_database_warning.Rafael Mendonça França
-
Add
ActiveRecord::Encryption::MessagePackMessageSerializer.Serialize data to the MessagePack format, for efficient storage in binary columns.
The binary encoding requires around 30% less space than the base64 encoding used by the default serializer.
Donal McBreen
-
Add support for encrypting binary columns.
Ensure encryption and decryption pass
Type::Binary::Dataaround for binary data.Previously encrypting binary columns with the
ActiveRecord::Encryption::MessageSerializerincidentally worked for MySQL and SQLite, but not PostgreSQL.Donal McBreen
-
Deprecated
ENV["SCHEMA_CACHE"]in favor ofschema_cache_pathin the database configuration.Rafael Mendonça França
-
Add
ActiveRecord::Base.with_connectionas a shortcut for leasing a connection for a short duration.The leased connection is yielded, and for the duration of the block, any call to
ActiveRecord::Base.connectionwill yield that same connection.This is useful to perform a few database operations without causing a connection to be leased for the entire duration of the request or job.
Jean Boussier
-
Deprecate
config.active_record.warn_on_records_fetched_greater_thannow thatsql.active_recordnotification includes:row_countfield.Jason Nochlin
-
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope.
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries.
Example:
associated will use LEFT JOIN instead of using JOIN
Post.left_joins(:author).where.associated(:author)
```
*Saleh Alhaddad*
-
Fix an issue where
ActiveRecord::Encryptionconfigurations are not ready before the loading of Active Record models, when an application is eager loaded. As a result, encrypted attributes could be misconfigured in some cases.Maxime Réty
-
Deprecate defining an
enumwith keyword arguments.class Function > ApplicationRecord
BAD
enum color: [:red, :blue],
type: [:instance, :class]
GOOD
enum :color, [:red, :blue]
enum :type, [:instance, :class]
end
```
*Hartley McGuire*
-
Add
config.active_record.validate_migration_timestampsoption for validating migration timestamps.When set, validates that the timestamp prefix for a migration is no more than a day ahead of the timestamp associated with the current time. This is designed to prevent migrations prefixes from being hand-edited to future timestamps, which impacts migration generation and other migration commands.
Adrianna Chang
-
Properly synchronize
Mysql2Adapter#active?andTrilogyAdapter#active?.As well as
disconnect!andverify!.This generally isn't a big problem as connections must not be shared between threads, but is required when running transactional tests or system tests and could lead to a SEGV.
Jean Boussier
-
Support
:source_locationtag option for query log tags.config.active_record.query_log_tags << :source_locationCalculating the caller location is a costly operation and should be used primarily in development (note, there is also a
config.active_record.verbose_query_logsthat serves the same purpose) or occasionally on production for debugging purposes.fatkodima
-
Add an option to
ActiveRecord::Encryption::Encryptorto disable compression.Allow compression to be disabled by setting
compress: falseclass User encrypts :name, encryptor: ActiveRecord::Encryption::Encryptor.new(compress: false) endDonal McBreen
-
Deprecate passing strings to
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename.A
ActiveRecord::DatabaseConfigurations::DatabaseConfigobject should be passed instead.Rafael Mendonça França
-
Add
row_countfield tosql.active_recordnotification.This field returns the amount of rows returned by the query that emitted the notification.
This metric is useful in cases where one wants to detect queries with big result sets.
Marvin Bitterlich
-
Consistently raise an
ArgumentErrorwhen passing an invalid argument to a nested attributes association writer.Previously, this would only raise on collection associations and produce a generic error on singular associations.
Now, it will raise on both collection and singular associations.
Joshua Young
-
Fix single quote escapes on default generated MySQL columns.
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression.
Previously, the schema dump would output a string with double escapes for generated columns with single quotes in the default expression.
This would result in issues when importing the schema on a fresh instance of a MySQL database.
Now, the string will not be escaped and will be valid Ruby upon importing of the schema.
Yash Kapadia
-
Fix Migrations with versions older than 7.1 validating options given to
add_referenceandt.references.Hartley McGuire
-
Add
<role>_typesclass method toActiveRecord::DelegatedTypeso that the delegated types can be introspected.JP Rosevear
-
Make
schema_dump,query_cache,replicaanddatabase_tasksconfigurable viaDATABASE_URL.This wouldn't always work previously because boolean values would be interpreted as strings.
e.g.
DATABASE_URL=postgres://localhost/foo?schema_dump=falsenow properly disable dumping the schema cache.Mike Coutermarsh, Jean Boussier
-
Introduce
ActiveRecord::Transactions::ClassMethods#set_callback.It is identical to
ActiveSupport::Callbacks::ClassMethods#set_callbackbut with support forafter_commitandafter_rollbackcallback options.Joshua Young
-
Make
ActiveRecord::Encryption::Encryptoragnostic of the serialization format used for encrypted data.Previously, the encryptor instance only allowed an encrypted value serialized as a
Stringto be passed to the message serializer.Now, the encryptor lets the configured
message_serializerdecide which types of serialized encrypted values are supported. A custom serialiser is therefore allowed to serializeActiveRecord::Encryption::Messageobjects using a type other thanString.The default
ActiveRecord::Encryption::MessageSerializeralready ensures that onlyStringobjects are passed for deserialization.Maxime Réty
-
Fix
encrypted_attribute?to take into account context properties passed toencrypts.Maxime Réty
-
The object returned by
explainnow responds topluck,first,last,average,count,maximum,minimum, andsum. Those new methods runEXPLAINon the corresponding queries:User.all.explain.count
EXPLAIN SELECT COUNT(*) FROM users
...
User.all.explain.maximum(:id)
EXPLAIN SELECT MAX(users.id) FROM users
...
```
*Petrik de Heus*
-
Fixes an issue where
validates_associated:onoption wasn't respected when validating associated records.Austen Madden, Alex Ghiculescu, Rafał Brize
-
Allow overriding SQLite defaults from
database.yml.Any PRAGMA configuration set under the
pragmaskey in the configuration file takes precedence over Rails' defaults, and additional PRAGMAs can be set as well.database: storage/development.sqlite3 timeout: 5000 pragmas: journal_mode: off temp_store: memoryStephen Margheim
-
Remove warning message when running SQLite in production, but leave it unconfigured.
There are valid use cases for running SQLite in production. However, it must be done with care, so instead of a warning most users won't see anyway, it's preferable to leave the configuration commented out to force them to think about having the database on a persistent volume etc.
Jacopo Beschi, Jean Boussier
-
Add support for generated columns to the SQLite3 adapter.
Generated columns (both stored and dynamic) are supported since version 3.31.0 of SQLite. This adds support for those to the SQLite3 adapter.
create_table :users do |t| t.string :name t.virtual :name_upper, type: :string, as: 'UPPER(name)' t.virtual :name_lower, type: :string, as: 'LOWER(name)', stored: true endStephen Margheim
-
TrilogyAdapter: ignore
hostifsocketparameter is set.This allows to configure a connection on a UNIX socket via
DATABASE_URL:DATABASE_URL=trilogy://does-not-matter/my_db_production?socket=/var/run/mysql.sockJean Boussier
-
Make
assert_queries_count,assert_no_queries,assert_queries_match, andassert_no_queries_matchassertions public.To assert the expected number of queries are made, Rails internally uses
assert_queries_countandassert_no_queries. To assert that specific SQL queries are made,assert_queries_matchandassert_no_queries_matchare used. These assertions can now be used in applications as well.class ArticleTest < ActiveSupport::TestCase test "queries are made" do assert_queries_count(1) { Article.first } end test "creates a foreign key" do assert_queries_match(/ADD FOREIGN KEY/i, include_schema: true) do @​connection.add_foreign_key(:comments, :posts) end end endPetrik de Heus, fatkodima
-
Fix
has_secure_tokencalls the setter method on initialize.Abeid Ahmed
-
When using a
DATABASE_URL, allow for a configuration to map the protocol in the URL to a specific database adapter. This allows decoupling the adapter the application chooses to use from the database connection details set in the deployment environment.
ENV['DATABASE_URL'] = "mysql://localhost/example_database"
config.active_record.protocol_adapters.mysql = "trilogy"
will connect to MySQL using the trilogy adapter
```
*Jean Boussier*, *Kevin McPhillips*
-
In cases where MySQL returns
warning_countgreater than zero, but returns no warnings when theSHOW WARNINGSquery is executed,ActiveRecord.db_warnings_actionproc will still be called with a generic warning message rather than silently ignoring the warning(s).Kevin McPhillips
-
DatabaseConfigurations#configs_foraccepts a symbol in thenameparameter.Andrew Novoselac
-
Fix
where(field: values)queries whenfieldis a serialized attribute (for example, whenfieldusesActiveRecord::Base.serializeor is a JSON column).João Alves
-
Make the output of
ActiveRecord::Core#inspectconfigurable.By default, calling
inspecton a record will yield a formatted string including just theid.Post.first.inspect #=> "#<Post id: 1>"The attributes to be included in the output of
inspectcan be configured withActiveRecord::Core#attributes_for_inspect.Post.attributes_for_inspect = [:id, :title] Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!">"With
attributes_for_inspectset to:all,inspectwill list all the record's attributes.Post.attributes_for_inspect = :all Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"In
developmentandtestmode,attributes_for_inspectwill be set to:allby default.You can also call
full_inspectto get an inspection with all the attributes.The attributes in
attribute_for_inspectwill also be used forpretty_print.Andrew Novoselac
-
Don't mark attributes as changed when reassigned to
Float::INFINITYor-Float::INFINITY.Maicol Bentancor
-
Support the
RETURNINGclause for MariaDB.fatkodima, Nikolay Kondratyev
-
The SQLite3 adapter now implements the
supports_deferrable_constraints?contract.Allows foreign keys to be deferred by adding the
:deferrablekey to theforeign_keyoptions.add_reference :person, :alias, foreign_key: { deferrable: :deferred } add_reference :alias, :person, foreign_key: { deferrable: :deferred }Stephen Margheim
-
Add the
set_constraintshelper to PostgreSQL connections.Post.create!(user_id: -1) # => ActiveRecord::InvalidForeignKey Post.transaction do Post.connection.set_constraints(:deferred) p = Post.create!(user_id: -1) u = User.create! p.user = u p.save! endCody Cutrer
-
Include
ActiveModel::APIinActiveRecord::Base.Sean Doyle
-
Ensure
#signed_idoutputsurl_safestrings.Jason Meller
-
Add
nulls_lastand workingdesc.nulls_firstfor MySQL.Tristan Fellows
-
Allow for more complex hash arguments for
orderwhich mimicswhereinActiveRecord::Relation.Topic.includes(:posts).order(posts: { created_at: :desc })Myles Boone
Action View
-
Fix templates with strict locals to also include
local_assigns.Previously templates defining strict locals wouldn't receive the
local_assignshash.Jean Boussier
-
Add queries count to template rendering instrumentation.
Before
Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms | Allocations: 112788)
After
Completed 200 OK in 3804ms (Views: 41.0ms | ActiveRecord: 33.5ms (2 queries, 1 cached) | Allocations: 112788)
```
*fatkodima*
-
Raise
ArgumentErrorif:renderableobject does not respond to#render_in.Sean Doyle
-
Add the
nonce: trueoption forstylesheet_link_taghelper to support automatic nonce generation for Content Security Policy.Works the same way as
javascript_include_tag nonce: truedoes.Akhil G Krishnan, AJ Esler
-
Parse
ActionView::TestCase#renderedHTML content asNokogiri::XML::DocumentFragmentinstead ofNokogiri::XML::Document.Sean Doyle
-
Rename
ActionView::TestCase::Behavior::ContenttoActionView::TestCase::Behavior::RenderedViewContent.Make
RenderedViewContentinherit fromString. Make private API with:nodoc:Sean Doyle
-
Deprecate passing
nilas value for themodel:argument to theform_withmethod.Collin Jilbert
-
Alias
field_set_taghelper tofieldset_tagto match<fieldset>element.Sean Doyle
-
Deprecate passing content to void elements when using
tag.brtype tag builders.Hartley McGuire
-
Fix the
number_to_human_sizeview helper to correctly work with negative numbers.Earlopain
-
Automatically discard the implicit locals injected by collection rendering for template that can't accept them.
When rendering a collection, two implicit variables are injected, which breaks templates with strict locals.
Now they are only passed if the template will actually accept them.
Yasha Krasnou, Jean Boussier
-
Fix
@rails/ujscallingstart()an extra time when using bundlers.Hartley McGuire, Ryunosuke Sato
-
Fix the
captureview helper compatibility with HAML and Slim.When a blank string was captured in HAML or Slim (and possibly other template engines) it would instead return the entire buffer.
Jean Boussier
-
Updated
@rails/ujsfiles to ignore certain data-* attributes when element is contenteditable.This fix was already landed in >= 7.0.4.3, < 7.1.0. [CVE-2023-23913]
Ryunosuke Sato
-
Added validation for HTML tag names in the
tagandcontent_taghelper method.The
tagandcontent_tagmethod now checks that the provided tag name adheres to the HTML specification. If an invalid HTML tag name is provided, the method raises anArgumentErrorwith an appropriate error message.Examples:
Raises ArgumentError: Invalid HTML5 tag name: 12p
content_tag("12p") # Starting with a number
Raises ArgumentError: Invalid HTML5 tag name: ""
content_tag("") # Empty tag name
Raises ArgumentError: Invalid HTML5 tag name: div/
tag("div/") # Contains a solidus
Raises ArgumentError: Invalid HTML5 tag name: "image file"
tag("image file") # Contains a space
```
*Akhil G Krishnan*
Action Pack
-
Allow bots to ignore
allow_browser.Matthew Nguyen
-
Include the HTTP Permissions-Policy on non-HTML Content-Types [CVE-2024-28103]
Aaron Patterson, Zack Deveau
-
Fix
Mime::Type.parsehandling type parameters for HTTP Accept headers.Taylor Chaparro
-
Fix the error page that is displayed when a view template is missing to account for nested controller paths in the suggested correct location for the missing template.
Joshua Young
-
Add
save_and_open_pagehelper toIntegrationTest.save_and_open_pageis a helpful helper to keep a short feedback loop when working on system tests. A similar helper with matching signature has been added to integration tests.Joé Dupuis
-
Fix a regression in 7.1.3 passing a
to:option without a controller when the controller is already defined by a scope.Rails.application.routes.draw do controller :home do get "recent", to: "recent_posts" end endÉtienne Barrié
-
Request Forgery takes relative paths into account.
Stefan Wienert
-
Add ".test" as a default allowed host in development to ensure smooth golden-path setup with puma.dev.
DHH
-
Add
allow_browserto set minimum browser versions for the application.A browser that's blocked will by default be served the file in
public/406-unsupported-browser.htmlwith a HTTP status code of "406 Not Acceptable".class ApplicationController < ActionController::Base
Allow only browsers natively supporting webp images, web push, badges, import maps, CSS nesting + :has
allow_browser versions: :modern
end
class ApplicationController < ActionController::Base
All versions of Chrome and Opera will be allowed, but no versions of "internet explorer" (ie). Safari needs to be 16.4+ and Firefox 121+.
allow_browser versions: { safari: 16.4, firefox: 121, ie: false }
end
class MessagesController < ApplicationController
In addition to the browsers blocked by ApplicationController, also block Opera below 104 and Chrome below 119 for the show action.
allow_browser versions: { opera: 104, chrome: 119 }, only: :show
end
```
*DHH*
-
Add rate limiting API.
class SessionsController < ApplicationController rate_limit to: 10, within: 3.minutes, only: :create end class SignupsController < ApplicationController rate_limit to: 1000, within: 10.seconds, by: -> { request.domain }, with: -> { redirect_to busy_controller_url, alert: "Too many signups!" }, only: :new endDHH, Jean Boussier
-
Add
image/svg+xmlto the compressible content types ofActionDispatch::Static.Georg Ledermann
-
Add instrumentation for
ActionController::Live#send_stream.Allows subscribing to
send_streamevents. The event payload contains the filename, disposition, and type.Hannah Ramadan
-
Add support for
with_routingtest helper inActionDispatch::IntegrationTest.Gannon McGibbon
-
Remove deprecated support to set
Rails.application.config.action_dispatch.show_exceptionstotrueandfalse.Rafael Mendonça França
-
Remove deprecated
speaker,vibrate, andvrpermissions policy directives.Rafael Mendonça França
-
Remove deprecated
Rails.application.config.action_dispatch.return_only_request_media_type_on_content_type.Rafael Mendonça França
-
Deprecate
Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality.Rafael Mendonça França
-
Remove deprecated comparison between
ActionController::ParametersandHash.Rafael Mendonça França
-
Remove deprecated constant
AbstractController::Helpers::MissingHelperError.Rafael Mendonça França
-
Fix a race condition that could cause a
Text file busy - chromedrivererror with parallel system tests.Matt Brictson
-
Add
raccas a dependency since it will become a bundled gem in Ruby 3.4.0Hartley McGuire
-
Remove deprecated constant
ActionDispatch::IllegalStateError.Rafael Mendonça França
-
Add parameter filter capability for redirect locations.
It uses the
config.filter_parametersto match what needs to be filtered. The result would be like this:Redirected to http://secret.foo.bar?username=roque&password=[FILTERED]Fixes #14055.
Roque Pinel, Trevor Turk, tonytonyjan
Active Job
-
All tests now respect the
active_job.queue_adapterconfig.Previously if you had set
config.active_job.queue_adapterin yourconfig/application.rborconfig/environments/test.rbfile, the adapter you selected was previously not used consistently across all tests. In some tests your adapter would be used, but other tests would use theTestAdapter.In Rails 7.2, all tests will respect the
queue_adapterconfig if provided. If no config is provided, theTestAdapterwill continue to be used.See #48585 for more details.
Alex Ghiculescu
-
Make Active Job transaction aware when used conjointly with Active Record.
A common mistake with Active Job is to enqueue jobs from inside a transaction, causing them to potentially be picked and ran by another process, before the transaction is committed, which may result in various errors.
Topic.transaction do topic = Topic.create(...) NewTopicNotificationJob.perform_later(topic) endNow Active Job will automatically defer the enqueuing to after the transaction is committed, and drop the job if the transaction is rolled back.
Various queue implementations can choose to disable this behavior, and users can disable it, or force it on a per job basis:
class NewTopicNotificationJob < ApplicationJob self.enqueue_after_transaction_commit = :never # or `:always` or `:default` endJean Boussier, Cristian Bica
-
Do not trigger immediate loading of
ActiveJob::Basewhen loadingActiveJob::TestHelper.Maxime Réty
-
Preserve the serialized timezone when deserializing
ActiveSupport::TimeWithZonearguments.Joshua Young
-
Remove deprecated
:exponentially_longervalue for the:waitinretry_on.Rafael Mendonça França
-
Remove deprecated support to set numeric values to
scheduled_atattribute.Rafael Mendonça França
-
Deprecate
Rails.application.config.active_job.use_big_decimal_serialize.Rafael Mendonça França
-
Remove deprecated primitive serializer for
BigDecimalarguments.Rafael Mendonça França
Action Mailer
-
Remove deprecated params via
:argsforassert_enqueued_email_with.Rafael Mendonça França
-
Remove deprecated
config.action_mailer.preview_path.Rafael Mendonça França
Action Cable
-
Bring
ActionCable::Connection::TestCookieJarin alignment withActionDispatch::Cookies::CookieJarin regards to setting the cookie value.Before:
cookies[:foo] = { value: "bar" } puts cookies[:foo] # => { value: "bar" }After:
cookies[:foo] = { value: "bar" } puts cookies[:foo] # => "bar"Justin Ko
-
Record ping on every Action Cable message.
Previously only
pingandwelcomemessage types were keeping the connection active. Now every Action Cable message updates thepingedAtvalue, preventing the connection from being marked as stale.yauhenininjia
-
Add two new assertion methods for Action Cable test cases:
assert_has_no_streamandassert_has_no_stream_for.These methods can be used to assert that a stream has been stopped, e.g. via
stop_streamorstop_stream_for. They complement the already existingassert_has_streamandassert_has_stream_formethods.assert_has_no_stream "messages" assert_has_no_stream_for User.find(42)Sebastian Pöll, Junichi Sato
Active Storage
-
Remove deprecated
config.active_storage.silence_invalid_content_types_warning.Rafael Mendonça França
-
Remove deprecated
config.active_storage.replace_on_assign_to_many.Rafael Mendonça França
-
Add support for custom
keyinActiveStorage::Blob#compose.Elvin Efendiev
-
Add
image/webptoconfig.active_storage.web_image_content_typeswhenload_defaults "7.2"is set.Lewis Buckley
-
Fix JSON-encoding of
ActiveStorage::Filenameinstances.Jonathan del Strother
-
Fix N+1 query when fetching preview images for non-image assets.
Aaron Patterson & Justin Searls
-
Fix all Active Storage database related models to respect
ActiveRecord::Base.table_name_prefixconfiguration.Chedli Bourguiba
-
Fix
ActiveStorage::Representations::ProxyControllernot returning the proper preview image variant for previewable files.Chedli Bourguiba
-
Fix
ActiveStorage::Representations::ProxyControllerto proxy untracked variants.Chedli Bourguiba
-
When using the
preprocessed: trueoption, avoid enqueuing transform jobs for blobs that are not representable.Chedli Bourguiba
-
Prevent
ActiveStorage::Blob#previewto generate a variant if an empty variation is passed.Calls to
#url,#keyor#downloadwill now use the original preview image instead of generating a variant with the exact same dimensions.Chedli Bourguiba
-
Process preview image variant when calling
ActiveStorage::Preview#processed.For example,
attached_pdf.preview(:thumb).processedwill now immediately generate the full-sized preview image and the:thumbvariant of it. Previously, the:thumbvariant would not be generated until a further call to e.g.processed.url.Chedli Bourguiba and Jonathan Hefner
-
Prevent
ActiveRecord::StrictLoadingViolationErrorwhen strict loading is enabled and the variant of an Active Storage preview has already been processed (for example, by callingActiveStorage::Preview#url).Jonathan Hefner
-
Fix
preprocessed: trueoption for named variants of previewable files.Nico Wenterodt
-
Allow accepting
serviceas a proc as well inhas_one_attachedandhas_many_attached.Yogesh Khater
Action Mailbox
-
Fix all Action Mailbox database related models to respect
ActiveRecord::Base.table_name_prefixconfiguration.Chedli Bourguiba
Action Text
-
Only sanitize
contentattribute when present in attachments.Petrik de Heus
-
Sanitize ActionText HTML ContentAttachment in Trix edit view [CVE-2024-32464]
Aaron Patterson, Zack Deveau
-
Use
includesinstead ofeager_loadforwith_all_rich_text.Petrik de Heus
-
Delegate
ActionText::Content#deconstructtoNokogiri::XML::DocumentFragment#elements.content = ActionText::Content.new <<~HTML <h1>Hello, world</h1> <div>The body</div> HTML content => [h1, div] assert_pattern { h1 => { content: "Hello, world" } } assert_pattern { div => { content: "The body" } }Sean Doyle
-
Fix all Action Text database related models to respect
ActiveRecord::Base.table_name_prefixconfiguration.Chedli Bourguiba
-
Compile ESM package that can be used directly in the browser as actiontext.esm.js
Matias Grunberg
-
Fix using actiontext.js with Sprockets.
Matias Grunberg
-
Upgrade Trix to 2.0.7
Hartley McGuire
-
Fix using Trix with Sprockets.
Hartley McGuire
Railties
-
The new
bin/rails bootcommand boots the application and exits. Supports the standard-e/--environmentoptions.Xavier Noria
-
Create a Dev Container Generator that generates a Dev Container setup based on the current configuration of the application. Usage:
bin/rails devcontainerAndrew Novoselac
-
Add Rubocop and GitHub Actions to plugin generator. This can be skipped using --skip-rubocop and --skip-ci.
Chris Oliver
-
Remove support for
oracle,sqlserverand JRuby specific database adapters from therails newandrails db:system:changecommands.The supported options are
sqlite3,mysql,postgresqlandtrilogy.Andrew Novoselac
-
Add options to
bin/rails app:update.bin/rails app:updatenow supports the same generic options that generators do:-
--force: Accept all changes to existing files -
--skip: Refuse all changes to existing files -
--pretend: Don't make any changes -
--quiet: Don't output all changes made
Étienne Barrié
-
-
Implement Rails console commands and helpers with IRB v1.13's extension APIs.
Rails console users will now see
helper,controller,new_session, andappunder IRB help message'sHelper methodscategory. Andreload!command will be displayed under the newRails consolecommands category.Prior to this change, Rails console's commands and helper methods are added through IRB's private components and don't show up in its help message, which led to poor discoverability.
Stan Lo
-
Remove deprecated
Rails::Generators::Testing::Behaviour.Rafael Mendonça França
-
Remove deprecated
find_cmd_and_execconsole helper.Rafael Mendonça França
-
Remove deprecated
Rails.config.enable_dependency_loading.Rafael Mendonça França
-
Remove deprecated
Rails.application.secrets.Rafael Mendonça França
-
Generated Gemfile will include
require: "debug/prelude"for thedebuggem.Requiring
debuggem directly automatically activates it, which could introduce additional overhead and memory usage even without entering a debugging session.By making Bundler require
debug/preludeinstead, developers can keep their access to breakpoint methods likedebuggerorbinding.break, but the debugger won't be activated until a breakpoint is hit.Stan Lo
-
Skip generating a
testjob in ci.yml when a new application is generated with the--skip-testoption.Steve Polito
-
Update the
.node-versionfile conditionally generated for new applications to 20.11.1Steve Polito
-
Fix sanitizer vendor configuration in 7.1 defaults.
In apps where rails-html-sanitizer was not eagerly loaded, the sanitizer default could end up being Rails::HTML4::Sanitizer when it should be set to Rails::HTML5::Sanitizer.
Mike Dalessio, Rafael Mendonça França
-
Set
action_mailer.default_url_optionsvalues indevelopmentandtest.Prior to this commit, new Rails applications would raise
ActionView::Template::Errorif a mailer included a url built with a*_pathhelper.Steve Polito
-
Introduce
Rails::Generators::Testing::Assertions#assert_initializer.Compliments the existing
initializergenerator action.assert_initializer "mail_interceptors.rb"Steve Polito
-
Generate a .devcontainer folder and its contents when creating a new app.
The .devcontainer folder includes everything needed to boot the app and do development in a remote container.
The container setup includes:
- A redis container for Kredis, ActionCable etc.
- A database (SQLite, Postgres, MySQL or MariaDB)
- A Headless chrome container for system tests
- Active Storage configured to use the local disk and with preview features working
If any of these options are skipped in the app setup they will not be included in the container configuration.
These files can be skipped using the
--skip-devcontaineroption.Andrew Novoselac & Rafael Mendonça França
-
Introduce
SystemTestCase#served_byfor configuring the System Test application server.By default this is localhost. This method allows the host and port to be specified manually.
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase served_by host: "testserver", port: 45678 endAndrew Novoselac & Rafael Mendonça França
-
bin/rails testwill no longer load files named*_test.rbif they are located in thefixturesfolder.Edouard Chin
-
Ensure logger tags configured with
config.log_tagsare still active inrequest.action_dispatchhandlers.KJ Tsanaktsidis
-
Setup jemalloc in the default Dockerfile for memory optimization.
Matt Almeida, Jean Boussier
-
Commented out lines in .railsrc file should not be treated as arguments when using rails new generator command. Update ARGVScrubber to ignore text after
#symbols.Willian Tenfen
-
Skip CSS when generating APIs.
Ruy Rocha
-
Rails console now indicates application name and the current Rails environment:
my-app(dev)> # for RAILS_ENV=development my-app(test)> # for RAILS_ENV=test my-app(prod)> # for RAILS_ENV=production my-app(my_env)> # for RAILS_ENV=my_envThe application name is derived from the application's module name from
config/application.rb. For example,MyAppwill displayed asmy-appin the prompt.Additionally, the environment name will be colorized when the environment is
development(blue),test(blue), orproduction(red), if your terminal supports it.Stan Lo
-
Ensure
autoload_paths,autoload_once_paths,eager_load_paths, andload_pathsonly have directories when initialized from engine defaults.Previously, files under the
appdirectory could end up there too.Takumasa Ochi
-
Prevent unnecessary application reloads in development.
Previously, some files outside autoload paths triggered unnecessary reloads. With this fix, application reloads according to
Rails.autoloaders.main.dirs, thereby preventing unnecessary reloads.Takumasa Ochi
-
Use
oven-sh/setup-bunin GitHub CI when generating an app with Bun.TangRufus
-
Disable
pidfilegeneration in theproductionenvironment.Hans Schnedlitz
-
Set
config.action_view.annotate_rendered_view_with_filenamestotruein thedevelopmentenvironment.Adrian Marin
-
Support the
BACKTRACEenvironment variable to turn off backtrace cleaning.Useful for debugging framework code:
BACKTRACE=1 bin/rails serverAlex Ghiculescu
-
Raise
ArgumentErrorwhen readingconfig.x.somethingwith arguments:config.x.this_works.this_raises true # raises ArgumentErrorSean Doyle
-
Add default PWA files for manifest and service-worker that are served from
app/views/pwaand can be dynamically rendered through ERB. Mount these files explicitly at the root with default routes in the generated routes file.DHH
-
Updated system tests to now use headless Chrome by default for the new applications.
DHH
-
Add GitHub CI files for Dependabot, Brakeman, RuboCop, and running tests by default. Can be skipped with
--skip-ci.DHH
-
Add Brakeman by default for static analysis of security vulnerabilities. Allow skipping with
--skip-brakeman option.vipulnsward
-
Add RuboCop with rules from
rubocop-rails-omakaseby default. Skip with--skip-rubocop.DHH and zzak
-
Use
bin/rails runner --skip-executorto not wrap the runner script with an Executor.Ben Sheldon
-
Fix isolated engines to take
ActiveRecord::Base.table_name_prefixinto consideration.This will allow for engine defined models, such as inside Active Storage, to respect Active Record table name prefix configuration.
Chedli Bourguiba
-
Fix running
db:system:changewhen the app has no Dockerfile.Hartley McGuire
-
In Action Mailer previews, list inline attachments separately from normal attachments.
For example, attachments that were previously listed like
Attachments: logo.png file1.pdf file2.pdf
will now be listed like
Attachments: file1.pdf file2.pdf (Inline: logo.png)
Christian Schmidt and Jonathan Hefner
-
In mailer preview, only show SMTP-To if it differs from the union of To, Cc and Bcc.
Christian Schmidt
-
Enable YJIT by default on new applications running Ruby 3.3+.
This can be disabled by setting
Rails.application.config.yjit = falseJean Boussier, Rafael Mendonça França
-
In Action Mailer previews, show date from message
Dateheader if present.Sampat Badhe
-
Exit with non-zero status when the migration generator fails.
Katsuhiko YOSHIDA
-
Use numeric UID and GID in Dockerfile template.
The Dockerfile generated by
rails newsets the default user and group by name instead of UID:GID. This can cause the following error in Kubernetes:container has runAsNonRoot and image has non-numeric user (rails), cannot verify user is non-rootThis change sets default user and group by their numeric values.
Ivan Fedotov
-
Disallow invalid values for rails new options.
The
--database,--asset-pipeline,--css, and--javascriptoptions forrails newtake different arguments. This change validates them.Tony Drake, Akhil G Krishnan, Petrik de Heus
-
Conditionally print
$stdoutwhen invokingrun_generator.In an effort to improve the developer experience when debugging generator tests, we add the ability to conditionally print
$stdoutinstead of capturing it.This allows for calls to
binding.irbandputswork as expected.RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rbSteve Polito
-
Remove the option
config.public_file_server.enabledfrom the generators for all environments, as the value is the same in all environments.Adrian Hirt
v7.1.3.4: 7.1.3.4
Active Support
- No changes.
Active Model
- No changes.
Active Record
- No changes.
Action View
- No changes.
Action Pack
- Include the HTTP Permissions-Policy on non-HTML Content-Types [CVE-2024-28103]
Active Job
- No changes.
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
- Sanitize ActionText HTML ContentAttachment in Trix edit view [CVE-2024-32464]
Railties
- No changes.
v7.1.3.3: 7.1.3.3
Active Support
- No changes.
Active Model
- No changes.
Active Record
- No changes.
Action View
- No changes.
Action Pack
- No changes.
Active Job
- No changes.
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
-
Upgrade Trix to 2.1.1 to fix CVE-2024-34341.
Rafael Mendonça França
Railties
- No changes.
v7.1.3.2
Active Support
- No changes.
Active Model
- No changes.
Active Record
- No changes.
Action View
- No changes.
Action Pack
- Fix
raise_on_missing_translationsnot working correctly with thetranslatemethod in controllers after the patch for CVE-2024-26143.
Active Job
- No changes.
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
- No changes.
Railties
- No changes.
v7.1.3.1: 7.1.3.1
Active Support
- No changes.
Active Model
- No changes.
Active Record
- No changes.
Action View
- No changes.
Action Pack
-
Fix possible XSS vulnerability with the
translatemethod in controllersCVE-2024-26143
-
Fix ReDoS in Accept header parsing
CVE-2024-26142
Active Job
- No changes.
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
- No changes.
Railties
- No changes.
v7.1.3: 7.1.3
Active Support
-
Handle nil
backtrace_locationsinActiveSupport::SyntaxErrorProxy.Eugene Kenny
-
Fix
ActiveSupport::JSON.encodeto prevent duplicate keys.If the same key exist in both String and Symbol form it could lead to the same key being emitted twice.
Manish Sharma
-
Fix
ActiveSupport::Cache::Store#read_multiwhen using a cache namespace and local cache strategy.Mark Oleson
-
Fix
Time.now/DateTime.now/Date.todayto return results in a system timezone after#travel_to.There is a bug in the current implementation of #travel_to: it remembers a timezone of its argument, and all stubbed methods start returning results in that remembered timezone. However, the expected behaviour is to return results in a system timezone.
Aleksei Chernenkov
-
Fix
:unless_existoption forMemoryStore#write(et al) when using a cache namespace.S. Brent Faulkner
-
Fix ActiveSupport::Deprecation to handle blaming generated code.
Jean Boussier, fatkodima
Active Model
- No changes.
Active Record
-
Fix Migrations with versions older than 7.1 validating options given to
add_reference.Hartley McGuire
-
Ensure
reloadsets correct owner for each association.Dmytro Savochkin
-
Fix view runtime for controllers with async queries.
fatkodima
-
Fix
load_asyncto work with query cache.fatkodima
-
Fix polymorphic
belongs_toto correctly use parent'squery_constraints.fatkodima
-
Fix
Preloaderto not generate a query for already loaded association withquery_constraints.fatkodima
-
Fix multi-database polymorphic preloading with equivalent table names.
When preloading polymorphic associations, if two models pointed to two tables with the same name but located in different databases, the preloader would only load one.
Ari Summer
-
Fix
encrypted_attribute?to take into account context properties passed toencrypts.Maxime Réty
-
Fix
find_byto work correctly in presence of composite primary keys.fatkodima
-
Fix async queries sometimes returning a raw result if they hit the query cache.
ShipPart.async_countcould return a raw integer rather than a Promise if it found the result in the query cache.fatkodima
-
Fix
Relation#transactionto not apply a default scope.The method was incorrectly setting a default scope around its block:
Post.where(published: true).transaction do Post.count # SELECT COUNT(*) FROM posts WHERE published = FALSE; endJean Boussier
-
Fix calling
async_pluckon anonerelation.Model.none.async_pluck(:id)was returning a naked value instead of a promise.Jean Boussier
-
Fix calling
load_asyncon anonerelation.Model.none.load_asyncwas returning a broken result.Lucas Mazza
-
TrilogyAdapter: ignore
hostifsocketparameter is set.This allows to configure a connection on a UNIX socket via DATABASE_URL:
DATABASE_URL=trilogy://does-not-matter/my_db_production?socket=/var/run/mysql.sockJean Boussier
-
Fix
has_secure_tokencalls the setter method on initialize.Abeid Ahmed
-
Allow using
object_idas a database column name. It was available before rails 7.1 and may be used as a part of polymorphic relationship toobjectwhereobjectcan be any other database record.Mikhail Doronin
-
Fix
rails db:create:allto not touch databases before they are created.fatkodima
Action View
-
Better handle SyntaxError in Action View.
Mario Caropreso
-
Fix
word_wrapwith empty string.Jonathan Hefner
-
Rename
ActionView::TestCase::Behavior::ContenttoActionView::TestCase::Behavior::RenderedViewContent.Make
RenderedViewContentinherit fromString. Make private API with:nodoc:.Sean Doyle
-
Fix detection of required strict locals.
Further fix
render @​collectioncompatibility with strict localsJean Boussier
Action Pack
-
Fix including
Rails.application.routes.url_helpersdirectly in anActiveSupport::Concern.Jonathan Hefner
-
Fix system tests when using a Chrome binary that has been downloaded by Selenium.
Jonathan Hefner
Active Job
-
Do not trigger immediate loading of
ActiveJob::Basewhen loadingActiveJob::TestHelper.Maxime Réty
-
Preserve the serialized timezone when deserializing
ActiveSupport::TimeWithZonearguments.Joshua Young
-
Fix ActiveJob arguments serialization to correctly serialize String subclasses having custom serializers.
fatkodima
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
-
Fix N+1 query when fetching preview images for non-image assets.
Aaron Patterson & Justin Searls
-
Fix all Active Storage database related models to respect
ActiveRecord::Base.table_name_prefixconfiguration.Chedli Bourguiba
-
Fix
ActiveStorage::Representations::ProxyControllernot returning the proper preview image variant for previewable files.Chedli Bourguiba
-
Fix
ActiveStorage::Representations::ProxyControllerto proxy untracked variants.Chedli Bourguiba
-
Fix direct upload forms when submit button contains nested elements.
Marc Köhlbrugge
-
When using the
preprocessed: trueoption, avoid enqueuing transform jobs for blobs that are not representable.Chedli Bourguiba
-
Process preview image variant when calling
ActiveStorage::Preview#processed. For example,attached_pdf.preview(:thumb).processedwill now immediately generate the full-sized preview image and the:thumbvariant of it. Previously, the:thumbvariant would not be generated until a further call to e.g.processed.url.Chedli Bourguiba and Jonathan Hefner
-
Prevent
ActiveRecord::StrictLoadingViolationErrorwhen strict loading is enabled and the variant of an Active Storage preview has already been processed (for example, by callingActiveStorage::Preview#url).Jonathan Hefner
-
Fix
preprocessed: trueoption for named variants of previewable files.Nico Wenterodt
Action Mailbox
- No changes.
Action Text
- No changes.
Railties
-
Make sure
config.after_routes_loadedhook runs on boot.Rafael Mendonça França
-
Fix
config.log_levelnot being respected when using aBroadcastLoggerÉdouard Chin
-
Fix isolated engines to take
ActiveRecord::Base.table_name_prefixinto consideration. This will allow for engine defined models, such as inside Active Storage, to respect Active Record table name prefix configuration.Chedli Bourguiba
-
The
bin/rails app:templatecommand will no longer add potentially unwanted gem platforms viabundle lock --add-platform=...commands.Jonathan Hefner
v7.1.2: 7.1.2
Active Support
-
Fix
:expires_inoption forRedisCacheStore#write_multi.fatkodima
-
Fix deserialization of non-string "purpose" field in Message serializer
Jacopo Beschi
-
Prevent global cache options being overwritten when setting dynamic options inside a
ActiveSupport::Cache::Store#fetchblock.Yasha Krasnou
-
Fix missing
requireresulting inNoMethodErrorwhen runningbin/rails secrets:showorbin/rails secrets:edit.Stephen Ierodiaconou
-
Ensure
{down,up}case_firstreturns non-frozen string.Jonathan Hefner
-
Fix
#to_fs(:human_size)to correctly work with negative numbers.Earlopain
-
Fix
BroadcastLogger#dupso that it duplicates the logger'sbroadcasts.Andrew Novoselac
-
Fix issue where
bootstrap.rboverwrites thelevelof aBroadcastLogger'sbroadcasts.Andrew Novoselac
-
Fix
ActiveSupport::Cacheto handle outdated Marshal payload from Rails 6.1 format.Active Support's Cache is supposed to treat a Marshal payload that can no longer be deserialized as a cache miss. It fail to do so for compressed payload in the Rails 6.1 legacy format.
Jean Boussier
-
Fix
OrderedOptions#digfor array indexes.fatkodima
-
Fix time travel helpers to work when nested using with separate classes.
fatkodima
-
Fix
delete_matchedfor file cache store to work with keys longer than the max filename size.fatkodima and Jonathan Hefner
-
Fix compatibility with the
semantic_loggergem.The
semantic_loggergem doesn't behave exactly like stdlib logger in thatSemanticLogger#levelreturns a Symbol while stdlibLogger#levelreturns an Integer.This caused the various
LogSubscriberclasses in Rails to break when assigned aSemanticLoggerinstance.Jean Boussier, ojab
Active Model
-
Make
==(other)method of AttributeSet safe.Dmitry Pogrebnoy
Active Record
-
Fix renaming primary key index when renaming a table with a UUID primary key in PostgreSQL.
fatkodima
-
Fix
where(field: values)queries whenfieldis a serialized attribute (for example, whenfieldusesActiveRecord::Base.serializeor is a JSON column).João Alves
-
Prevent marking broken connections as verified.
Daniel Colson
-
Don't mark Float::INFINITY as changed when reassigning it
When saving a record with a float infinite value, it shouldn't mark as changed
Maicol Bentancor
-
ActiveRecord::Base.table_namenow returnsnilinstead of raising "undefined methodabstract_class?for Object:Class".a5-stable
-
Fix upserting for custom
:on_duplicateand:unique_byconsisting of all inserts keys.fatkodima
-
Fixed an issue where saving a record could innappropriately
dupits attributes.Jonathan Hefner
-
Dump schema only for a specific db for rollback/up/down tasks for multiple dbs.
fatkodima
-
Fix
NoMethodErrorwhen casting a PostgreSQLmoneyvalue that uses a comma as its radix point and has no leading currency symbol. For example, when casting"3,50".Andreas Reischuck and Jonathan Hefner
-
Re-enable support for using
enumwith non-column-backed attributes. Non-column-backed attributes must be previously declared with an explicit type. For example:class Post < ActiveRecord::Base attribute :topic, :string enum topic: %i[science tech engineering math] endJonathan Hefner
-
Raise on
foreign_key:being passed as an array in associationsNikita Vasilevsky
-
Return back maximum allowed PostgreSQL table name to 63 characters.
fatkodima
-
Fix detecting
IDENTITYcolumns for PostgreSQL < 10.fatkodima
Action View
-
Fix the
number_to_human_sizeview helper to correctly work with negative numbers.Earlopain
-
Automatically discard the implicit locals injected by collection rendering for template that can't accept them
When rendering a collection, two implicit variables are injected, which breaks templates with strict locals.
Now they are only passed if the template will actually accept them.
Yasha Krasnou, Jean Boussier
-
Fix
@rails/ujscallingstart()an extra time when using bundlersHartley McGuire, Ryunosuke Sato
-
Fix the
captureview helper compatibility with HAML and SlimWhen a blank string was captured in HAML or Slim (and possibly other template engines) it would instead return the entire buffer.
Jean Boussier
Action Pack
-
Fix a race condition that could cause a
Text file busy - chromedrivererror with parallel system testsMatt Brictson
-
Fix
StrongParameters#extract_valueto include blank valuesOtherwise composite parameters may not be parsed correctly when one of the component is blank.
fatkodima, Yasha Krasnou, Matthias Eiglsperger
-
Add
raccas a dependency since it will become a bundled gem in Ruby 3.4.0Hartley McGuire
-
Support handling Enumerator for non-buffered responses.
Zachary Scott
Active Job
- No changes.
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
-
Compile ESM package that can be used directly in the browser as actiontext.esm.js
Matias Grunberg
-
Fix using actiontext.js with Sprockets
Matias Grunberg
-
Upgrade Trix to 2.0.7
Hartley McGuire
-
Fix using Trix with Sprockets
Hartley McGuire
Railties
-
Fix running
db:system:changewhen app has no Dockerfile.Hartley McGuire
-
If you accessed
config.eager_load_pathsand friends, later changes toconfig.pathswere not reflected in the expected auto/eager load paths. Now, they are.This bug has been latent since Rails 3.
Fixes #49629.
Xavier Noria
v7.1.1: 7.1.1
Active Support
-
Add support for keyword arguments when delegating calls to custom loggers from
ActiveSupport::BroadcastLogger.Jenny Shen
-
NumberHelper: handle objects respondingto_d.fatkodima
-
Fix RedisCacheStore to properly set the TTL when incrementing or decrementing.
This bug was only impacting Redis server older than 7.0.
Thomas Countz
-
Fix MemoryStore to prevent race conditions when incrementing or decrementing.
Pierre Jambet
Active Model
- No changes.
Active Record
-
Fix auto populating IDENTITY columns for PostgreSQL.
fatkodima
-
Fix "ArgumentError: wrong number of arguments (given 3, expected 2)" when down migrating
rename_tablein older migrations.fatkodima
-
Do not require the Action Text, Active Storage and Action Mailbox tables to be present when running when running test on CI.
Rafael Mendonça França
Action View
-
Updated
@rails/ujsfiles to ignore certain data-* attributes when element is contenteditable.This fix was already landed in >= 7.0.4.3, < 7.1.0. [CVE-2023-23913]
Ryunosuke Sato
Action Pack
- No changes.
Active Job
-
Don't log enqueuing details when the job wasn't enqueued.
Dustin Brown
Action Mailer
- No changes.
Action Cable
- No changes.
Active Storage
- No changes.
Action Mailbox
- No changes.
Action Text
- No changes.
Railties
-
Ensures the Rails generated Dockerfile uses correct ruby version and matches Gemfile.
Abhay Nikam
v7.1.0: 7.1.0
Active Support
-
Fix
AS::MessagePackwithENV["RAILS_MAX_THREADS"].Jonathan Hefner
-
Add a new public API for broadcasting logs
This feature existed for a while but was until now a private API. Broadcasting log allows to send log message to difference sinks (STDOUT, a file ...) and is used by default in the development environment to write logs both on STDOUT and in the "development.log" file.
Basic usage:
stdout_logger = Logger.new(STDOUT) file_logger = Logger.new("development.log") broadcast = ActiveSupport::BroadcastLogger.new(stdout_logger, file_logger) broadcast.info("Hello!") # The "Hello!" message is written on STDOUT and in the log file.Adding other sink(s) to the broadcast:
broadcast = ActiveSupport::BroadcastLogger.new broadcast.broadcast_to(Logger.new(STDERR))Remove a sink from the broadcast:
stdout_logger = Logger.new(STDOUT) broadcast = ActiveSupport::BroadcastLogger.new(stdout_logger) broadcast.stop_broadcasting_to(stdout_logger)Edouard Chin
-
Fix Range#overlap? not taking empty ranges into account on Ruby < 3.3
Nobuyoshi Nakada, Shouichi Kamiya, Hartley McGuire
-
Use Ruby 3.3 Range#overlap? if available
Yasuo Honda
-
Add
bigdecimalas Active Support dependency that is a bundled gem candidate for Ruby 3.4.bigdecimal3.1.4 or higher version will be installed. Ruby 2.7 and 3.0 users who wantbigdecimalversion 2.0.0 or 3.0.0 behavior as a default gem, pin thebigdecimalversion in your application Gemfile.Koichi ITO
-
Add
drb,mutex_mandbase64that are bundled gem candidates for Ruby 3.4Yasuo Honda
-
When using cache format version >= 7.1 or a custom serializer, expired and version-mismatched cache entries can now be detected without deserializing their values.
Jonathan Hefner
-
Make all cache stores return a boolean for
#deletePreviously the
RedisCacheStore#deletewould return1if the entry exists and0otherwise. Now it returns true if the entry exists and false otherwise, just like the other stores.The
FileStorewould returnnilif the entry doesn't exists and returnsfalsenow as well.Petrik de Heus
-
Active Support cache stores now support replacing the default compressor via a
:compressoroption. The specified compressor must respond todeflateandinflate. For example:module MyCompressor def self.deflate(string)
compression logic...
end
def self.inflate(compressed)
decompression logic...
end
end
config.cache_store = :redis_cache_store, { compressor: MyCompressor }
```
*Jonathan Hefner*
-
Active Support cache stores now support a
:serializeroption. Similar to the:coderoption, serializers must respond todumpandload. However, serializers are only responsible for serializing a cached value, whereas coders are responsible for serializing the entireActiveSupport::Cache::Entryinstance. Additionally, the output from serializers can be automatically compressed, whereas coders are responsible for their own compression.Specifying a serializer instead of a coder also enables performance optimizations, including the bare string optimization introduced by cache format version 7.1.
The
:serializerand:coderoptions are mutually exclusive. Specifying both will raise anArgumentError.Jonathan Hefner
-
Fix
ActiveSupport::Inflector.humanize(nil)raisingNoMethodError: undefined method `end_with?' for nil:NilClass.James Robinson
-
Don't show secrets for
ActiveSupport::KeyGenerator#inspect.Before:
ActiveSupport::KeyGenerator.new(secret).inspect "#<ActiveSupport::KeyGenerator:0x0000000104888038 ... @​secret=\"\\xAF\\bFh]LV}q\\nl\\xB2U\\xB3 ... >"After:
ActiveSupport::KeyGenerator::Aes256Gcm(secret).inspect "#<ActiveSupport::KeyGenerator:0x0000000104888038>"Petrik de Heus
-
Improve error message when EventedFileUpdateChecker is used without a compatible version of the Listen gem
Hartley McGuire
-
Add
:reportbehavior for DeprecationSetting
config.active_support.deprecation = :reportuses the error reporter to report deprecation warnings toActiveSupport::ErrorReporter.Deprecations are reported as handled errors, with a severity of
:warning.Useful to report deprecations happening in production to your bug tracker.
Étienne Barrié
-
Rename
Range#overlaps?to#overlap?and add alias for backwards compatibilityChristian Schmidt
-
Fix
EncryptedConfigurationreturning incorrect values for someHashmethodsHartley McGuire
-
Don't show secrets for
MessageEncryptor#inspect.Before:
ActiveSupport::MessageEncryptor.new(secret, cipher: "aes-256-gcm").inspect "#<ActiveSupport::MessageEncryptor:0x0000000104888038 ... @​secret=\"\\xAF\\bFh]LV}q\\nl\\xB2U\\xB3 ... >"After:
ActiveSupport::MessageEncryptor.new(secret, cipher: "aes-256-gcm").inspect "#<ActiveSupport::MessageEncryptor:0x0000000104888038>"Petrik de Heus
-
Don't show contents for
EncryptedConfiguration#inspect.Before:
Rails.application.credentials.inspect "#<ActiveSupport::EncryptedConfiguration:0x000000010d2b38e8 ... @​config={:secret=>\"something secret\"} ... @​key_file_contents=\"915e4ea054e011022398dc242\" ...>"After:
Rails.application.credentials.inspect "#<ActiveSupport::EncryptedConfiguration:0x000000010d2b38e8>"Petrik de Heus
-
ERB::Util.html_escape_oncealways returns anhtml_safestring.This method previously maintained the
html_safe?property of a string on the return value. Because this string has been escaped, however, not marking it ashtml_safecauses entities to be double-escaped.As an example, take this view snippet:
<p><%= html_escape_once("this & that & the other") %></p>Before this change, that would be double-escaped and render as:
<p>this &amp; that &amp; the other</p>After this change, it renders correctly as:
<p>this & that & the other</p>Fixes #48256
Mike Dalessio
-
Deprecate
SafeBuffer#clone_empty.This method has not been used internally since Rails 4.2.0.
Mike Dalessio
-
MessageEncryptor,MessageVerifier, andconfig.active_support.message_serializernow accept:message_packand:message_pack_allow_marshalas serializers. These serializers require themsgpackgem (>= 1.7.0).The Message Pack format can provide improved performance and smaller payload sizes. It also supports round-tripping some Ruby types that are not supported by JSON. For example:
verifier = ActiveSupport::MessageVerifier.new("secret") data = [{ a: 1 }, { b: 2 }.with_indifferent_access, 1.to_d, Time.at(0, 123)] message = verifier.generate(data)
BEFORE with config.active_support.message_serializer = :json
verifier.verified(message)
=> [{"a"=>1}, {"b"=>2}, "1.0", "1969-12-31T18:00:00.000-06:00"]
verifier.verified(message).map(&:class)
=> [Hash, Hash, String, String]
AFTER with config.active_support.message_serializer = :message_pack
verifier.verified(message)
=> [{:a=>1}, {"b"=>2}, 0.1e1, 1969-12-31 18:00:00.000123 -0600]
verifier.verified(message).map(&:class)
=> [Hash, ActiveSupport::HashWithIndifferentAccess, BigDecimal, Time]
```
The `:message_pack` serializer can fall back to deserializing with
`ActiveSupport::JSON` when necessary, and the `:message_pack_allow_marshal`
serializer can fall back to deserializing with `Marshal` as well as
`ActiveSupport::JSON`. Additionally, the `:marshal`, `:json`, and
`:json_allow_marshal` serializers can now fall back to deserializing with
`ActiveSupport::MessagePack` when necessary. These behaviors ensure old
messages can still be read so that migration is easier.
*Jonathan Hefner*
-
A new
7.1cache format is available which includes an optimization for bare string values such as view fragments.The
7.1cache format is used by default for new apps, and existing apps can enable the format by settingconfig.load_defaults 7.1or by settingconfig.active_support.cache_format_version = 7.1inconfig/application.rbor aconfig/environments/*.rbfile.Cache entries written using the
6.1or7.0cache formats can be read when using the7.1format. To perform a rolling deploy of a Rails 7.1 upgrade, wherein servers that have not yet been upgraded must be able to read caches from upgraded servers, leave the cache format unchanged on the first deploy, then enable the7.1cache format on a subsequent deploy.Jonathan Hefner
-
Active Support cache stores can now use a preconfigured serializer based on
ActiveSupport::MessagePackvia the:serializeroption:config.cache_store = :redis_cache_store, { serializer: :message_pack }The
:message_packserializer can reduce cache entry sizes and improve performance, but requires themsgpackgem (>= 1.7.0).The
:message_packserializer can read cache entries written by the default serializer, and the default serializer can now read entries written by the:message_packserializer. These behaviors make it easy to migrate between serializer without invalidating the entire cache.Jonathan Hefner
-
Object#deep_dupno longer duplicate named classes and modules.Before:
hash = { class: Object, module: Kernel } hash.deep_dup # => {:class=>#<Class:0x00000001063ffc80>, :module=>#<Module:0x00000001063ffa00>}After:
hash = { class: Object, module: Kernel } hash.deep_dup # => {:class=>Object, :module=>Kernel}Jean Boussier
-
Consistently raise an
ArgumentErrorif theActiveSupport::Cachekey is blank.Joshua Young
-
Deprecate usage of the singleton
ActiveSupport::Deprecation.All usage of
ActiveSupport::Deprecationas a singleton is deprecated, the most common one beingActiveSupport::Deprecation.warn. Gem authors should now create their own deprecator (ActiveSupport::Deprecationobject), and use it to emit deprecation warnings.Calling any of the following without specifying a deprecator argument is also deprecated:
- Module.deprecate
- deprecate_constant
- DeprecatedObjectProxy
- DeprecatedInstanceVariableProxy
- DeprecatedConstantProxy
- deprecation-related test assertions
Use of
ActiveSupport::Deprecation.silenceand configuration methods likebehavior=,disallowed_behavior=,disallowed_warnings=should now be aimed at the application's deprecators.Rails.application.deprecators.silence do
code that emits deprecation warnings
end
```
If your gem has a Railtie or Engine, it's encouraged to add your deprecator to the application's deprecators, that
way the deprecation related configuration options will apply to it as well, e.g.
`config.active_support.report_deprecations` set to `false` in the production environment will also disable your
deprecator.
```ruby
initializer "my_gem.deprecator" do |app|
app.deprecators[:my_gem] = MyGem.deprecator
end
```
*Étienne Barrié*
-
Add
Object#withto set and restore public attributes around a blockclient.timeout # => 5 client.with(timeout: 1) do client.timeout # => 1 end client.timeout # => 5Jean Boussier
-
Remove deprecated support to generate incorrect RFC 4122 UUIDs when providing a namespace ID that is not one of the constants defined on
Digest::UUID.Rafael Mendonça França
-
Deprecate
config.active_support.use_rfc4122_namespaced_uuids.Rafael Mendonça França
-
Remove implicit conversion of objects into
StringbyActiveSupport::SafeBuffer.Rafael Mendonça França
-
Remove deprecated
active_support/core_ext/range/include_time_with_zonefile.Rafael Mendonça França
-
Deprecate
config.active_support.remove_deprecated_time_with_zone_name.Rafael Mendonça França
-
Remove deprecated override of
ActiveSupport::TimeWithZone.name.Rafael Mendonça França
-
Deprecate
config.active_support.disable_to_s_conversion.Rafael Mendonça França
-
Remove deprecated option to passing a format to
#to_sinArray,Range,Date,DateTime,Time,BigDecimal,Floatand,Integer.Rafael Mendonça França
-
Remove deprecated
ActiveSupport::PerThreadRegistry.Rafael Mendonça França
-
Remove deprecated override of
Enumerable#sum.Rafael Mendonça França
-
Deprecated initializing a
ActiveSupport::Cache::MemCacheStorewith an instance ofDalli::Client.Deprecate the undocumented option of providing an already-initialized instance of
Dalli::ClienttoActiveSupport::Cache::MemCacheStore. Such clients could be configured with unrecognized options, which could lead to unexpected behavior. Instead, provide addresses as documented.aledustet
-
Stub
Time.new()inTimeHelpers#travel_totravel_to Time.new(2004, 11, 24) do
Inside the travel_to block Time.new is stubbed
assert_equal 2004, Time.new.year
end
```
*fatkodima*
-
Raise
ActiveSupport::MessageEncryptor::InvalidMessagefromActiveSupport::MessageEncryptor#decrypt_and_verifyregardless of cipher. Previously, when aMessageEncryptorwas using a non-AEAD cipher such as AES-256-CBC, a corrupt or tampered message would raiseActiveSupport::MessageVerifier::InvalidSignature. Now, all ciphers raise the same error:encryptor = ActiveSupport::MessageEncryptor.new("x" * 32, cipher: "aes-256-gcm") message = encryptor.encrypt_and_sign("message") encryptor.decrypt_and_verify(message.next)
=> raises ActiveSupport::MessageEncryptor::InvalidMessage
encryptor = ActiveSupport::MessageEncryptor.new("x" * 32, cipher: "aes-256-cbc")
message = encryptor.encrypt_and_sign("message")
encryptor.decrypt_and_verify(message.next)
BEFORE:
=> raises ActiveSupport::MessageVerifier::InvalidSignature
AFTER:
=> raises ActiveSupport::MessageEncryptor::InvalidMessage
```
*Jonathan Hefner*
-
Support
niloriginal values when usingActiveSupport::MessageVerifier#verify. Previously,MessageVerifier#verifydid not work withniloriginal values, though bothMessageVerifier#verifiedandMessageEncryptor#decrypt_and_verifydo:encryptor = ActiveSupport::MessageEncryptor.new(secret) message = encryptor.encrypt_and_sign(nil) encryptor.decrypt_and_verify(message)
=> nil
verifier = ActiveSupport::MessageVerifier.new(secret)
message = verifier.generate(nil)
verifier.verified(message)
=> nil
verifier.verify(message)
BEFORE:
=> raises ActiveSupport::MessageVerifier::InvalidSignature
AFTER:
=> nil
```
*Jonathan Hefner*
-
Maintain
html_safe?on html_safe strings when sliced withslice,slice!, orchrmethod.Previously,
html_safe?was only maintained when the html_safe strings were sliced with[]method. Now,slice,slice!, andchrmethods will maintainhtml_safe?like[]method.string = "<div>test</div>".html_safe string.slice(0, 1).html_safe? # => true string.slice!(0, 1).html_safe? # => true
maintain html_safe? after the slice!
string.html_safe? # => true
string.chr.html_safe? # => true
```
*Michael Go*
-
Add
Object#in?support for open ranges.assert Date.today.in?(..Date.tomorrow) assert_not Date.today.in?(Date.tomorrow..)Ignacio Galindo
-
config.i18n.raise_on_missing_translations = truenow raises on any missing translation.Previously it would only raise when called in a view or controller. Now it will raise anytime
I18n.tis provided an unrecognised key.If you do not want this behaviour, you can customise the i18n exception handler. See the upgrading guide or i18n guide for more information.
Alex Ghiculescu
-
ActiveSupport::CurrentAttributesnow raises if a restricted attribute name is used.Attributes such as
setandresetcannot be used as they clash with theCurrentAttributespublic API.Alex Ghiculescu
-
HashWithIndifferentAccess#transform_keysnow takes a Hash argument, just as Ruby'sHash#transform_keysdoes.Akira Matsuda
-
delegatenow defines method with proper arity when delegating to a Class. With this change, it defines faster method (3.5x faster with no argument). However, in order to gain this benefit, the delegation target method has to be defined before declaring the delegation.
This defines 3.5 times faster method than before
class C
def self.x() end
delegate :x, to: :class
end
class C
This works but silently falls back to old behavior because
delegate cannot find the definition of x
delegate :x, to: :class
def self.x() end
end
```
*Akira Matsuda*
-
assert_differencemessage now includes what changed.This makes it easier to debug non-obvious failures.
Before:
"User.count" didn't change by 32. Expected: 1611 Actual: 1579After:
"User.count" didn't change by 32, but by 0. Expected: 1611 Actual: 1579Alex Ghiculescu
-
Add ability to match exception messages to
assert_raisesassertionInstead of this
error = assert_raises(ArgumentError) do perform_service(param: 'exception') end assert_match(/incorrect param/i, error.message)you can now write this
assert_raises(ArgumentError, match: /incorrect param/i) do perform_service(param: 'exception') endfatkodima
-
Add
Rails.env.local?shorthand forRails.env.development? || Rails.env.test?.DHH
-
ActiveSupport::Testing::TimeHelpersnow accepts namedwith_usecargument tofreeze_time,travel, andtravel_tomethods. Passing true prevents truncating the destination time withchange(usec: 0).KevSlashNull, and serprex
-
ActiveSupport::CurrentAttributes.resetsnow accepts a method nameThe block API is still the recommended approach, but now both APIs are supported:
class Current < ActiveSupport::CurrentAttributes resets { Time.zone = nil } resets :clear_time_zone endAlex Ghiculescu
-
Ensure
ActiveSupport::Testing::Isolation::Forkingcloses pipesPreviously,
Forking.run_in_isolationopened two ends of a pipe. The fork process closed the read end, wrote to it, and then terminated (which presumably closed the file descriptors on its end). The parent process closed the write end, read from it, and returned, never closing the read end.This resulted in an accumulation of open file descriptors, which could cause errors if the limit is reached.
Sam Bostock
-
Fix
Time#changeandTime#advancefor times around the end of Daylight Saving Time.Previously, when
Time#changeorTime#advanceconstructed a time inside the final stretch of Daylight Saving Time (DST), the non-DST offset would always be chosen for local times:
DST ended just before 2021-11-07 2:00:00 AM in US/Eastern.
ENV["TZ"] = "US/Eastern"
time = Time.local(2021, 11, 07, 00, 59, 59) + 1
=> 2021-11-07 01:00:00 -0400
time.change(day: 07)
=> 2021-11-07 01:00:00 -0500
time.advance(seconds: 0)
=> 2021-11-07 01:00:00 -0500
time = Time.local(2021, 11, 06, 01, 00, 00)
=> 2021-11-06 01:00:00 -0400
time.change(day: 07)
=> 2021-11-07 01:00:00 -0500
time.advance(days: 1)
=> 2021-11-07 01:00:00 -0500
```
And the DST offset would always be chosen for times with a `TimeZone`
object:
```ruby
Time.zone = "US/Eastern"
time = Time.new(2021, 11, 07, 02, 00, 00, Time.zone) - 3600
=> 2021-11-07 01:00:00 -0500
time.change(day: 07)
=> 2021-11-07 01:00:00 -0400
time.advance(seconds: 0)
=> 2021-11-07 01:00:00 -0400
time = Time.new(2021, 11, 8, 01, 00, 00, Time.zone)
=> 2021-11-08 01:00:00 -0500
time.change(day: 07)
=> 2021-11-07 01:00:00 -0400
time.advance(days: -1)
=> 2021-11-07 01:00:00 -0400
```
Now, `Time#change` and `Time#advance` will choose the offset that matches
the original time's offset when possible:
```ruby
ENV["TZ"] = "US/Eastern"
time = Time.local(2021, 11, 07, 00, 59, 59) + 1
=> 2021-11-07 01:00:00 -0400
time.change(day: 07)
=> 2021-11-07 01:00:00 -0400
time.advance(seconds: 0)
=> 2021-11-07 01:00:00 -0400
time = Time.local(2021, 11, 06, 01, 00, 00)
=> 2021-11-06 01:00:00 -0400
time.change(day: 07)
=> 2021-11-07 01:00:00 -0400
time.advance(days: 1)
=> 2021-11-07 01:00:00 -0400
Time.zone = "US/Eastern"
time = Time.new(2021, 11, 07, 02, 00, 00, Time.zone) - 3600
=> 2021-11-07 01:00:00 -0500
time.change(day: 07)
=> 2021-11-07 01:00:00 -0500
time.advance(seconds: 0)
=> 2021-11-07 01:00:00 -0500
time = Time.new(2021, 11, 8, 01, 00, 00, Time.zone)
=> 2021-11-08 01:00:00 -0500
time.change(day: 07)
=> 2021-11-07 01:00:00 -0500
time.advance(days: -1)
=> 2021-11-07 01:00:00 -0500
```
*Kevin Hall*, *Takayoshi Nishida*, and *Jonathan Hefner*
-
Fix MemoryStore to preserve entries TTL when incrementing or decrementing
This is to be more consistent with how MemCachedStore and RedisCacheStore behaves.
Jean Boussier
-
Rails.error.handleandRails.error.recordfilter now by multiple error classes.Rails.error.handle(IOError, ArgumentError) do 1 + '1' # raises TypeError end 1 + 1 # TypeErrors are not IOErrors or ArgumentError, so this will *not* be handledMartin Spickermann
-
Class#subclassesandClass#descendantsnow automatically filter reloaded classes.Previously they could return old implementations of reloadable classes that have been dereferenced but not yet garbage collected.
They now automatically filter such classes like
DescendantTracker#subclassesandDescendantTracker#descendants.Jean Boussier
-
Rails.error.reportnow marks errors as reported to avoid reporting them twice.In some cases, users might want to report errors explicitly with some extra context before letting it bubble up.
This also allows to safely catch and report errors outside of the execution context.
Jean Boussier
-
Add
assert_error_reportedandassert_no_error_reportedAllows to easily asserts an error happened but was handled
report = assert_error_reported(IOError) do
...
end
assert_equal "Oops", report.error.message
assert_equal "admin", report.context[:section]
assert_equal :warning, report.severity
assert_predicate report, :handled?
```
*Jean Boussier*
-
ActiveSupport::Deprecationbehavior callbacks can now receive the deprecator instance as an argument. This makes it easier for such callbacks to change their behavior based on the deprecator's state. For example, based on the deprecator'sdebugflag.3-arity and splat-args callbacks such as the following will now be passed the deprecator instance as their third argument:
->(message, callstack, deprecator) { ... }->(*args) { ... }->(message, *other_args) { ... }
2-arity and 4-arity callbacks such as the following will continue to behave the same as before:
->(message, callstack) { ... }->(message, callstack, deprecation_horizon, gem_name) { ... }->(message, callstack, *deprecation_details) { ... }
Jonathan Hefner
-
ActiveSupport::Deprecation#disallowed_warningsnow affects the instance on which it is configured.This means that individual
ActiveSupport::Deprecationinstances can be configured with their own disallowed warnings, and the globalActiveSupport::Deprecation.disallowed_warningsnow only affects the globalActiveSupport::Deprecation.warn.Before
ActiveSupport::Deprecation.disallowed_warnings = ["foo"] deprecator = ActiveSupport::Deprecation.new("2.0", "MyCoolGem") deprecator.disallowed_warnings = ["bar"] ActiveSupport::Deprecation.warn("foo") # => raise ActiveSupport::DeprecationException ActiveSupport::Deprecation.warn("bar") # => print "DEPRECATION WARNING: bar" deprecator.warn("foo") # => raise ActiveSupport::DeprecationException deprecator.warn("bar") # => print "DEPRECATION WARNING: bar"After
ActiveSupport::Deprecation.disallowed_warnings = ["foo"] deprecator = ActiveSupport::Deprecation.new("2.0", "MyCoolGem") deprecator.disallowed_warnings = ["bar"] ActiveSupport::Deprecation.warn("foo") # => raise ActiveSupport::DeprecationException ActiveSupport::Deprecation.warn("bar") # => print "DEPRECATION WARNING: bar" deprecator.warn("foo") # => print "DEPRECATION WARNING: foo" deprecator.warn("bar") # => raise ActiveSupport::DeprecationExceptionNote that global
ActiveSupport::Deprecationmethods such asActiveSupport::Deprecation.warnandActiveSupport::Deprecation.disallowed_warningshave been deprecated.Jonathan Hefner
-
Add italic and underline support to
ActiveSupport::LogSubscriber#colorPreviously, only bold text was supported via a positional argument. This allows for bold, italic, and underline options to be specified for colored logs.
info color("Hello world!", :red, bold: true, underline: true)Gannon McGibbon
-
Add
String#downcase_firstmethod.This method is the corollary of
String#upcase_first.Mark Schneider
-
thread_mattr_accessorwill call.dup.freezeon non-frozen default values.This provides a basic level of protection against different threads trying to mutate a shared default object.
Jonathan Hefner
-
Add
raise_on_invalid_cache_expiration_timeconfig toActiveSupport::Cache::StoreSpecifies if an
ArgumentErrorshould be raised ifRails.cachefetchorwriteare given an invalidexpires_atorexpires_intime.Options are
true, andfalse. Iffalse, the exception will be reported ashandledand logged instead. Defaults totrueifconfig.load_defaults >= 7.1.Trevor Turk
-
ActiveSupport::Cache:Store#fetchnow passes an options accessor to the block.It makes possible to override cache options:
Rails.cache.fetch("3rd-party-token") do |name, options| token = fetch_token_from_remote
set cache's TTL to match token's TTL
options.expires_in = token.expires_in
token
end
*Andrii Gladkyi*, *Jean Boussier*
-
defaultoption ofthread_mattr_accessornow applies through inheritance and also across new threads.Previously, the
defaultvalue provided was set only at the moment of defining the attribute writer, which would cause the attribute to be uninitialized in descendants and in other threads.Fixes #43312.
Thierry Deo
-
Redis cache store is now compatible with redis-rb 5.0.
Jean Boussier
-
Add
skip_nil:support toActiveSupport::Cache::Store#fetch_multi.Daniel Alfaro
-
Add
quartermethod to date/timeMatt Swanson
-
Fix
NoMethodErroron customActiveSupport::Deprecationbehavior.ActiveSupport::Deprecation.behavior=was supposed to accept any object that responds tocall, but in fact its internal implementation assumed that this object could respond toarity, so it was restricted to onlyProcobjects.This change removes this
arityrestriction of custom behaviors.Ryo Nakamura
-
Support
:url_safeoption forMessageEncryptor.The
MessageEncryptorconstructor now accepts a:url_safeoption, similar to theMessageVerifierconstructor. When enabled, this option ensures that messages use a URL-safe encoding.Jonathan Hefner
-
Add
url_safeoption toActiveSupport::MessageVerifierinitializerActiveSupport::MessageVerifier.newnow takes optionalurl_safeargument. It can generate URL-safe strings by passingurl_safe: true.verifier = ActiveSupport::MessageVerifier.new(url_safe: true) message = verifier.generate(data) # => URL-safe stringThis option is
falseby default to be backwards compatible.Shouichi Kamiya
-
Enable connection pooling by default for
MemCacheStoreandRedisCacheStore.If you want to disable connection pooling, set
:pooloption tofalsewhen configuring the cache store:config.cache_store = :mem_cache_store, "cache.example.com", pool: falsefatkodima
-
Add
force:support toActiveSupport::Cache::Store#fetch_multi.fatkodima
-
Deprecated
:pool_sizeand:pool_timeoutoptions for configuring connection pooling in cache stores.Use
pool: trueto enable pooling with default settings:config.cache_store = :redis_cache_store, pool: trueOr pass individual options via
:pooloption:config.cache_store = :redis_cache_store, pool: { size: 10, timeout: 2 }fatkodima
-
Allow #increment and #decrement methods of
ActiveSupport::Cache::Storesubclasses to set new values.Previously incrementing or decrementing an unset key would fail and return nil. A default will now be assumed and the key will be created.
Andrej Blagojević, Eugene Kenny
-
Add
skip_nil:support toRedisCacheStoreJoey Paris
-
ActiveSupport::Cache::MemoryStore#write(name, val, unless_exist:true)now correctly writes expired keys.Alan Savage
-
ActiveSupport::ErrorReporternow accepts and forward asource:parameter.This allow libraries to signal the origin of the errors, and reporters to easily ignore some sources.
Jean Boussier
-
Fix and add protections for XSS in
ActionView::HelpersandERB::Util.Add the method
ERB::Util.xml_name_escapeto escape dangerous characters in names of tags and names of attributes, following the specification of XML.Álvaro Martín Fraguas
-
Respect
ActiveSupport::Logger.new's:formatterkeyword argumentThe stdlib
Logger::newallows passing a:formatterkeyword argument to set the logger's formatter. PreviouslyActiveSupport::Logger.newignored that argument by always setting the formatter to an instance ofActiveSupport::Logger::SimpleFormatter.Steven Harman
-
Deprecate preserving the pre-Ruby 2.4 behavior of
to_timeWith Ruby 2.4+ the default for +to_time+ changed from converting to the local system time to preserving the offset of the receiver. At the time Rails supported older versions of Ruby so a compatibility layer was added to assist in the migration process. From Rails 5.0 new applications have defaulted to the Ruby 2.4+ behavior and since Rails 7.0 now only supports Ruby 2.7+ this compatibility layer can be safely removed.
To minimize any noise generated the deprecation warning only appears when the setting is configured to
falseas that is the only scenario where the removal of the compatibility layer has any effect.Andrew White
-
Pathname.blank?only returns true forPathname.new("")Previously it would end up calling
Pathname#empty?which returned true if the path existed and was an empty directory or file.That behavior was unlikely to be expected.
Jean Boussier
-
Deprecate
Notification::Event's#childrenand#parent_of?John Hawthorn
-
Change the default serializer of
ActiveSupport::MessageVerifierfromMarshaltoActiveSupport::JSONwhen usingconfig.load_defaults 7.1.Messages serialized with
Marshalcan still be read, but new messages will be serialized withActiveSupport::JSON. For more information, see https://guides.rubyonrails.org/v7.1/configuring.html#config-active-support-message-serializer.Saba Kiaei, David Buckley, and Jonathan Hefner
-
Change the default serializer of
ActiveSupport::MessageEncryptorfromMarshaltoActiveSupport::JSONwhen usingconfig.load_defaults 7.1.Messages serialized with
Marshalcan still be read, but new messages will be serialized withActiveSupport::JSON. For more information, see https://guides.rubyonrails.org/v7.1/configuring.html#config-active-support-message-serializer.Zack Deveau, Martin Gingras, and Jonathan Hefner
-
Add
ActiveSupport::TestCase#stub_constto stub a constant for the duration of a yield.DHH
-
Fix
ActiveSupport::EncryptedConfigurationto be compatible with Psych 4Stephen Sugden
-
Improve
File.atomic_writeerror handlingDaniel Pepper
-
Fix
Class#descendantsandDescendantsTracker#descendantscompatibility with Ruby 3.1.The native
Class#descendantswas reverted prior to Ruby 3.1 release, butClass#subclasseswas kept, breaking the feature detection.Jean Boussier
Active Model
-
Remove change in the typography of user facing error messages. For example, “can’t be blank” is again “can't be blank”.
Rafael Mendonça França
-
Support composite identifiers in
to_keyto_keyavoids wrapping#idvalue into anArrayif#idalready an arrayNikita Vasilevsky
-
Add
ActiveModel::Conversion.param_delimiterto configure delimiter being used into_paramNikita Vasilevsky
-
undefine_attribute_methodsundefines alias attribute methods along with attribute methods.Nikita Vasilevsky
-
Error.full_message now strips ":base" from the message.
zzak
-
Add a load hook for
ActiveModel::Model(namedactive_model) to match the load hook forActiveRecord::Baseand allow for overriding aspects of theActiveModel::Modelclass.Lewis Buckley
-
Improve password length validation in ActiveModel::SecurePassword to consider byte size for BCrypt compatibility.
The previous password length validation only considered the character count, which may not accurately reflect the 72-byte size limit imposed by BCrypt. This change updates the validation to consider both character count and byte size while keeping the character length validation in place.
user = User.new(password: "a" * 73) # 73 characters user.valid? # => false user.errors[:password] # => ["is too long"] user = User.new(password: "あ" * 25) # 25 characters, 75 bytes user.valid? # => false user.errors[:password] # => ["is too long"]ChatGPT, Guillermo Iguaran
-
has_secure_passwordnow generates an#{attribute}_saltmethod that returns the salt used to compute the password digest. The salt will change whenever the password is changed, so it can be used to create single-use password reset tokens withgenerates_token_for:class User < ActiveRecord::Base has_secure_password generates_token_for :password_reset, expires_in: 15.minutes do password_salt&.last(10) end endLázaro Nixon
-
Improve typography of user facing error messages. In English contractions, the Unicode APOSTROPHE (
U+0027) is now RIGHT SINGLE QUOTATION MARK (U+2019). For example, "can't be blank" is now "can’t be blank".Jon Dufresne
-
Add class to
ActiveModel::MissingAttributeErrorerror message.Show which class is missing the attribute in the error message:
user = User.first user.pets.select(:id).first.user_id
=> ActiveModel::MissingAttributeError: missing attribute 'user_id' for Pet
```
*Petrik de Heus*
-
Raise
NoMethodErrorinActiveModel::Type::Value#as_jsonto avoid unpredictable results.Vasiliy Ermolovich
-
Custom attribute types that inherit from Active Model built-in types and do not override the
serializemethod will now benefit from an optimization when serializing attribute values for the database.For example, with a custom type like the following:
class DowncasedString < ActiveModel::Type::String def cast(value) super&.downcase end end ActiveRecord::Type.register(:downcased_string, DowncasedString) class User < ActiveRecord::Base attribute :email, :downcased_string end user = User.new(email: "FooBar@example.com")Serializing the
emailattribute for the database will be roughly twice as fast. More expensivecastoperations will likely see greater improvements.Jonathan Hefner
-
has_secure_passwordnow supports password challenges via apassword_challengeaccessor and validation.A password challenge is a safeguard to verify that the current user is actually the password owner. It can be used when changing sensitive model fields, such as the password itself. It is different than a password confirmation, which is used to prevent password typos.
When
password_challengeis set, the validation checks that the value's digest matches the currently persistedpassword_digest(i.e.password_digest_was).This allows a password challenge to be done as part of a typical
updatecall, just like a password confirmation. It also allows a password challenge error to be handled in the same way as other validation errors.For example, in the controller, instead of:
password_params = params.require(:password).permit( :password_challenge, :password, :password_confirmation, ) password_challenge = password_params.delete(:password_challenge) @​password_challenge_failed = !current_user.authenticate(password_challenge) if !@​password_challenge_failed && current_user.update(password_params)
...
end
```
You can now write:
```ruby
password_params = params.require(:password).permit(
:password_challenge,
:password,
:password_confirmation,
).with_defaults(password_challenge: "")
if current_user.update(password_params)
...
end
```
And, in the view, instead of checking `@password_challenge_failed`, you can
render an error for the `password_challenge` field just as you would for
other form fields, including utilizing `config.action_view.field_error_proc`.
*Jonathan Hefner*
-
Support infinite ranges for
LengthValidators:in/:withinoptionsvalidates_length_of :first_name, in: ..30fatkodima
-
Add support for beginless ranges to inclusivity/exclusivity validators:
validates_inclusion_of :birth_date, in: -> { (..Date.today) }validates_exclusion_of :birth_date, in: -> { (..Date.today) }Bo Jeanes
-
Make validators accept lambdas without record argument
Before
validates_comparison_of :birth_date, less_than_or_equal_to: ->(_record) { Date.today }
After
validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today }
```
*fatkodima*
-
Fix casting long strings to
Date,TimeorDateTimefatkodima
-
Use different cache namespace for proxy calls
Models can currently have different attribute bodies for the same method names, leading to conflicts. Adding a new namespace
:active_model_proxyfixes the issue.Chris Salzberg
Active Record
-
Remove -shm and -wal SQLite files when
rails db:dropis run.Niklas Häusele
-
Revert the change to raise an
ArgumentErrorwhen#accepts_nested_attributes_foris declared more than once for an association in the same class.The reverted behavior broke the case where the
#accepts_nested_attributes_forwas defined in a concern and where overridden in the class that included the concern.Rafael Mendonça França
-
Better naming for unique constraints support.
Naming unique keys leads to misunderstanding it's a short-hand of unique indexes. Just naming it unique constraints is not misleading.
In Rails 7.1.0.beta1 or before:
add_unique_key :sections, [:position], deferrable: :deferred, name: "unique_section_position" remove_unique_key :sections, name: "unique_section_position"Now:
add_unique_constraint :sections, [:position], deferrable: :deferred, name: "unique_section_position" remove_unique_constraint :sections, name: "unique_section_position"Ryuta Kamizono
-
Fix duplicate quoting for check constraint expressions in schema dump when using MySQL
A check constraint with an expression, that already contains quotes, lead to an invalid schema dump with the mysql2 adapter.
Fixes #42424.
Felix Tscheulin
-
Performance tune the SQLite3 adapter connection configuration
For Rails applications, the Write-Ahead-Log in normal syncing mode with a capped journal size, a healthy shared memory buffer and a shared cache will perform, on average, 2× better.
Stephen Margheim
-
Allow SQLite3
busy_handlerto be configured with simple max number ofretriesRetrying busy connections without delay is a preferred practice for performance-sensitive applications. Add support for a
database.ymlretriesinteger, which is used in a simplebusy_handlerfunction to retry busy connections without exponential backoff up to the max number ofretries.Stephen Margheim
-
The SQLite3 adapter now supports
supports_insert_returning?Implementing the full
supports_insert_returning?contract means the SQLite3 adapter supports auto-populated columns (#48241) as well as custom primary keys.Stephen Margheim
-
Ensure the SQLite3 adapter handles default functions with the
||concatenation operatorPreviously, this default function would produce the static string
"'Ruby ' || 'on ' || 'Rails'". Now, the adapter will appropriately receive and use"Ruby on Rails".change_column_default "test_models", "ruby_on_rails", -> { "('Ruby ' || 'on ' || 'Rails')" }Stephen Margheim
-
Dump PostgreSQL schemas as part of the schema dump.
Lachlan Sylvester
-
Encryption now supports
support_unencrypted_databeing set per-attribute.You can now opt out of
support_unencrypted_dataon a specific encrypted attribute. This only has an effect ifActiveRecord::Encryption.config.support_unencrypted_data == true.class User < ActiveRecord::Base encrypts :name, deterministic: true, support_unencrypted_data: false encrypts :email, deterministic: true endAlex Ghiculescu
-
Add instrumentation for Active Record transactions
Allows subscribing to transaction events for tracking/instrumentation. The event payload contains the connection and the outcome (commit, rollback, restart, incomplete), as well as timing details.
ActiveSupport::Notifications.subscribe("transaction.active_record") do |event| puts "Transaction event occurred!" connection = event.payload[:connection] puts "Connection: #{connection.inspect}" endDaniel Colson, Ian Candy
-
Support composite foreign keys via migration helpers.
Assuming "carts" table has "(shop_id, user_id)" as a primary key.
add_foreign_key(:orders, :carts, primary_key: [:shop_id, :user_id])
remove_foreign_key(:orders, :carts, primary_key: [:shop_id, :user_id])
foreign_key_exists?(:orders, :carts, primary_key: [:shop_id, :user_id])
```
*fatkodima*
-
Adds support for
if_not_existswhen adding a check constraint.add_check_constraint :posts, "post_type IN ('blog', 'comment', 'share')", if_not_exists: trueCody Cutrer
-
Raise an
ArgumentErrorwhen#accepts_nested_attributes_foris declared more than once for an association in the same class. Previously, the last declaration would silently override the previous one. Overriding in a subclass is still allowed.Joshua Young
-
Deprecate
rewhereargument on#merge.The
rewhereargument on#mergeis deprecated without replacement and will be removed in Rails 7.2.Adam Hess
-
Fix unscope is not working in specific case
Before:
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts` WHERE `posts`.`id` >= 1 AND `posts`.`id` < 3"After:
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts`"Fixes #48094.
Kazuya Hatanaka
-
Change
has_secure_tokendefault toon: :initializeChange the new default value from
on: :createtoon: :initializeCan be controlled by the
config.active_record.generate_secure_token_onconfiguration:config.active_record.generate_secure_token_on = :createSean Doyle
-
Fix
change_columnnot settingprecision: 6ondatetimecolumns when using 7.0+ Migrations and SQLite.Hartley McGuire
-
Support composite identifiers in
to_keyto_keyavoids wrapping#idvalue into anArrayif#idalready an arrayNikita Vasilevsky
-
Add validation option for
enumclass Contract < ApplicationRecord enum :status, %w[in_progress completed], validate: true end Contract.new(status: "unknown").valid? # => false Contract.new(status: nil).valid? # => false Contract.new(status: "completed").valid? # => true class Contract < ApplicationRecord enum :status, %w[in_progress completed], validate: { allow_nil: true } end Contract.new(status: "unknown").valid? # => false Contract.new(status: nil).valid? # => true Contract.new(status: "completed").valid? # => trueEdem Topuzov, Ryuta Kamizono
-
Allow batching methods to use already loaded relation if available
Calling batch methods on already loaded relations will use the records previously loaded instead of retrieving them from the database again.
Adam Hess
-
Deprecate
read_attribute(:id)returning the primary key if the primary key is not:id.Starting in Rails 7.2,
read_attribute(:id)will return the value of the id column, regardless of the model's primary key. To retrieve the value of the primary key, use#idinstead.read_attribute(:id)for composite primary key models will now return the value of the id column.Adrianna Chang
-
Fix
change_tablesetting datetime precision for 6.1 MigrationsHartley McGuire
-
Fix change_column setting datetime precision for 6.1 Migrations
Hartley McGuire
-
Add
ActiveRecord::Base#id_valuealias to access the raw value of a record's id column.This alias is only provided for models that declare an
:idcolumn.Adrianna Chang
-
Fix previous change tracking for
ActiveRecord::Storewhen using a column with JSON structured database typeBefore, the methods to access the changes made during the last save
#saved_change_to_key?,#saved_change_to_key, and#key_before_last_savedid not work if the store was defined as astore_accessoron a column with a JSON structured database typeRobert DiMartino
-
Fully support
NULLS [NOT] DISTINCTfor PostgreSQL 15+ indexes.Previous work was done to allow the index to be created in a migration, but it was not supported in schema.rb. Additionally, the matching for
NULLS [NOT] DISTINCTwas not in the correct order, which could have resulted in inconsistent schema detection.Gregory Jones
-
Allow escaping of literal colon characters in
sanitize_sql_*methods when named bind variables are usedJustin Bull
-
Fix
#previously_new_record?to return true for destroyed records.Before, if a record was created and then destroyed,
#previously_new_record?would return true. Now, any UPDATE or DELETE to a record is considered a change, and will result in#previously_new_record?returning false.Adrianna Chang
-
Specify callback in
has_secure_tokenclass User < ApplicationRecord has_secure_token on: :initialize end User.new.token # => "abc123...."Sean Doyle
-
Fix incrementation of in memory counter caches when associations overlap
When two associations had a similarly named counter cache column, Active Record could sometime increment the wrong one.
Jacopo Beschi, Jean Boussier
-
Don't show secrets for Active Record's
Cipher::Aes256Gcm#inspect.Before:
ActiveRecord::Encryption::Cipher::Aes256Gcm.new(secret).inspect "#<ActiveRecord::Encryption::Cipher::Aes256Gcm:0x0000000104888038 ... @​secret=\"\\xAF\\bFh]LV}q\\nl\\xB2U\\xB3 ... >"After:
ActiveRecord::Encryption::Cipher::Aes256Gcm(secret).inspect "#<ActiveRecord::Encryption::Cipher::Aes256Gcm:0x0000000104888038>"Petrik de Heus
-
Bring back the historical behavior of committing transaction on non-local return.
Model.transaction do model.save return other_model.save # not executed endHistorically only raised errors would trigger a rollback, but in Ruby
2.3, thetimeoutlibrary started usingthrowto interrupt execution which had the adverse effect of committing open transactions.To solve this, in Active Record 6.1 the behavior was changed to instead rollback the transaction as it was safer than to potentially commit an incomplete transaction.
Using
return,breakorthrowinside atransactionblock was essentially deprecated from Rails 6.1 onwards.However with the release of
timeout 0.4.0,Timeout.timeoutnow raises an error again, and Active Record is able to return to its original, less surprising, behavior.This historical behavior can now be opt-ed in via:
Rails.application.config.active_record.commit_transaction_on_non_local_return = trueAnd is the default for new applications created in Rails 7.1.
Jean Boussier
-
Deprecate
nameargument on#remove_connection.The
nameargument is deprecated on#remove_connectionwithout replacement.#remove_connectionshould be called directly on the class that established the connection.Eileen M. Uchitelle
-
Fix has_one through singular building with inverse.
Allows building of records from an association with a has_one through a singular association with inverse. For belongs_to through associations, linking the foreign key to the primary key model isn't needed. For has_one, we cannot build records due to the association not being mutable.
Gannon McGibbon
-
Disable database prepared statements when query logs are enabled
Prepared Statements and Query Logs are incompatible features due to query logs making every query unique.
zzak, Jean Boussier
-
Support decrypting data encrypted non-deterministically with a SHA1 hash digest.
This adds a new Active Record encryption option to support decrypting data encrypted non-deterministically with a SHA1 hash digest:
Rails.application.config.active_record.encryption.support_sha1_for_non_deterministic_encryption = trueThe new option addresses a problem when upgrading from 7.0 to 7.1. Due to a bug in how Active Record Encryption was getting initialized, the key provider used for non-deterministic encryption were using SHA-1 as its digest class, instead of the one configured globally by Rails via
Rails.application.config.active_support.key_generator_hash_digest_class.Cadu Ribeiro and Jorge Manrubia
-
Added PostgreSQL migration commands for enum rename, add value, and rename value.
rename_enumandrename_enum_valueare reversible. Due to Postgres limitation,add_enum_valueis not reversible since you cannot delete enum values. As an alternative you should drop and recreate the enum entirely.rename_enum :article_status, to: :article_stateadd_enum_value :article_state, "archived" # will be at the end of existing values add_enum_value :article_state, "in review", before: "published" add_enum_value :article_state, "approved", after: "in review"rename_enum_value :article_state, from: "archived", to: "deleted"Ray Faddis
-
Allow composite primary key to be derived from schema
Booting an application with a schema that contains composite primary keys will not issue warning and won't
nilify theActiveRecord::Base#primary_keyvalue anymore.Given a
travel_routestable definition and aTravelRoutemodel like:create_table :travel_routes, primary_key: [:origin, :destination], force: true do |t| t.string :origin t.string :destination end class TravelRoute < ActiveRecord::Base; endThe
TravelRoute.primary_keyvalue will be automatically derived to["origin", "destination"]Nikita Vasilevsky
-
Include the
connection_poolwith exceptions raised from an adapter.The
connection_poolprovides added context such as the connection used that led to the exception as well as which role and shard.Luan Vieira
-
Support multiple column ordering for
find_each,find_in_batchesandin_batches.When find_each/find_in_batches/in_batches are performed on a table with composite primary keys, ascending or descending order can be selected for each key.
Person.find_each(order: [:desc, :asc]) do |person| person.party_all_night! endTakuya Kurimoto
-
Fix where on association with has_one/has_many polymorphic relations.
Before:
Treasure.where(price_estimates: PriceEstimate.all) #=> SELECT (...) WHERE "treasures"."id" IN (SELECT "price_estimates"."estimate_of_id" FROM "price_estimates")Later:
Treasure.where(price_estimates: PriceEstimate.all) #=> SELECT (...) WHERE "treasures"."id" IN (SELECT "price_estimates"."estimate_of_id" FROM "price_estimates" WHERE "price_estimates"."estimate_of_type" = 'Treasure')Lázaro Nixon
-
Assign auto populated columns on Active Record record creation.
Changes record creation logic to allow for the
auto_incrementcolumn to be assigned immediately after creation regardless of it's relation to the model's primary key.The PostgreSQL adapter benefits the most from the change allowing for any number of auto-populated columns to be assigned on the object immediately after row insertion utilizing the
RETURNINGstatement.Nikita Vasilevsky
-
Use the first key in the
shardshash fromconnected_tofor thedefault_shard.Some applications may not want to use
:defaultas a shard name in their connection model. Unfortunately Active Record expects there to be a:defaultshard because it must assume a shard to get the right connection from the pool manager. Rather than force applications to manually set this,connects_tocan infer the default shard name from the hash of shards and will now assume that the first shard is your default.For example if your model looked like this:
class ShardRecord < ApplicationRecord self.abstract_class = true connects_to shards: { shard_one: { writing: :shard_one }, shard_two: { writing: :shard_two } }Then the
default_shardfor this class would be set toshard_one.Fixes: #45390
Eileen M. Uchitelle
-
Fix mutation detection for serialized attributes backed by binary columns.
Jean Boussier
-
Add
ActiveRecord.disconnect_all!method to immediately close all connections from all pools.Jean Boussier
-
Discard connections which may have been left in a transaction.
There are cases where, due to an error,
within_new_transactionmay unexpectedly leave a connection in an open transaction. In these cases the connection may be reused, and the following may occur:- Writes appear to fail when they actually succeed.
- Writes appear to succeed when they actually fail.
- Reads return stale or uncommitted data.
Previously, the following case was detected:
- An error is encountered during the transaction, then another error is encountered while attempting to roll it back.
Now, the following additional cases are detected:
- An error is encountered just after successfully beginning a transaction.
- An error is encountered while committing a transaction, then another error is encountered while attempting to roll it back.
- An error is encountered while rolling back a transaction.
Nick Dower
-
Active Record query cache now evicts least recently used entries
By default it only keeps the
100most recently used queries.The cache size can be configured via
database.ymldevelopment: adapter: mysql2 query_cache: 200It can also be entirely disabled:
development: adapter: mysql2 query_cache: falseJean Boussier
-
Deprecate
check_pending!in favor ofcheck_all_pending!.check_pending!will only check for pending migrations on the current database connection or the one passed in. This has been deprecated in favor ofcheck_all_pending!which will find all pending migrations for the database configurations in a given environment.Eileen M. Uchitelle
-
Make
increment_counter/decrement_counteraccept an amount argumentPost.increment_counter(:comments_count, 5, by: 3)fatkodima
-
Add support for
Array#intersect?toActiveRecord::Relation.Array#intersect?is only available on Ruby 3.1 or later.This allows the Rubocop
Style/ArrayIntersectcop to work withActiveRecord::Relationobjects.John Harry Kelly
-
The deferrable foreign key can be passed to
t.references.Hiroyuki Ishii
-
Deprecate
deferrable: trueoption ofadd_foreign_key.deferrable: trueis deprecated in favor ofdeferrable: :immediate, and will be removed in Rails 7.2.Because
deferrable: trueanddeferrable: :deferredare hard to understand. Both true and :deferred are truthy values. This behavior is the same as the deferrable option of the add_unique_key method, added in #46192.Hiroyuki Ishii
-
AbstractAdapter#executeand#exec_querynow clear the query cacheIf you need to perform a read only SQL query without clearing the query cache, use
AbstractAdapter#select_all.Jean Boussier
-
Make
.joins/.left_outer_joinswork with CTEs.For example:
Post .with(commented_posts: Comment.select(:post_id).distinct) .joins(:commented_posts) #=> WITH (...) SELECT ... INNER JOIN commented_posts on posts.id = commented_posts.post_idVladimir Dementyev
-
Add a load hook for
ActiveRecord::ConnectionAdapters::Mysql2Adapter(namedactive_record_mysql2adapter) to allow for overriding aspects of theActiveRecord::ConnectionAdapters::Mysql2Adapterclass. This makesMysql2Adapterconsistent withPostgreSQLAdapterandSQLite3Adapterthat already have load hooks.fatkodima
-
Introduce adapter for Trilogy database client
Trilogy is a MySQL-compatible database client. Rails applications can use Trilogy by configuring their
config/database.yml:development: adapter: trilogy database: blog_development pool: 5Or by using the
DATABASE_URLenvironment variable:ENV['DATABASE_URL'] # => "trilogy://localhost/blog_development?pool=5"Adrianna Chang
-
after_commitcallbacks defined on models now execute in the correct order.class User < ActiveRecord::Base after_commit { puts("this gets called first") } after_commit { puts("this gets called second") } endPreviously, the callbacks executed in the reverse order. To opt in to the new behaviour:
config.active_record.run_after_transaction_callbacks_in_order_defined = trueThis is the default for new apps.
Alex Ghiculescu
-
Infer
foreign_keywheninverse_ofis present onhas_oneandhas_manyassociations.has_many :citations, foreign_key: "book1_id", inverse_of: :bookcan be simplified to
has_many :citations, inverse_of: :bookand the foreign_key will be read from the corresponding
belongs_toassociation.Daniel Whitney
-
Limit max length of auto generated index names
Auto generated index names are now limited to 62 bytes, which fits within the default index name length limits for MySQL, Postgres and SQLite.
Any index name over the limit will fallback to the new short format.
Before (too long):
index_testings_on_foo_and_bar_and_first_name_and_last_name_and_administratorAfter (short format):
idx_on_foo_bar_first_name_last_name_administrator_5939248142The short format includes a hash to ensure the name is unique database-wide.
Mike Coutermarsh
-
Introduce a more stable and optimized Marshal serializer for Active Record models.
Can be enabled with
config.active_record.marshalling_format_version = 7.1.Jean Boussier
-
Allow specifying where clauses with column-tuple syntax.
Querying through
#wherenow accepts a new tuple-syntax which accepts, as a key, an array of columns and, as a value, an array of corresponding tuples. The key specifies a list of columns, while the value is an array of ordered-tuples that conform to the column list.For instance:
Cpk::Book => Cpk::Book(author_id: integer, number: integer, title: string, revision: integer)
Cpk::Book.primary_key => ["author_id", "number"]
book = Cpk::Book.create!(author_id: 1, number: 1)
Cpk::Book.where(Cpk::Book.primary_key => [[1, 2]]) # => [book]
Topic => Topic(id: integer, title: string, author_name: string...)
Topic.where([:title, :author_name] => [["The Alchemist", "Paulo Coelho"], ["Harry Potter", "J.K Rowling"]])
```
*Paarth Madan*
-
Allow warning codes to be ignore when reporting SQL warnings.
Active Record config that can ignore warning codes
Configure allowlist of warnings that should always be ignored
config.active_record.db_warnings_ignore = [
"1062", # MySQL Error 1062: Duplicate entry
]
```
This is supported for the MySQL and PostgreSQL adapters.
*Nick Borromeo*
-
Introduce
:active_record_fixtureslazy load hook.Hooks defined with this name will be run whenever
TestFixturesis included in a class.ActiveSupport.on_load(:active_record_fixtures) do self.fixture_paths << "test/fixtures" end klass = Class.new klass.include(ActiveRecord::TestFixtures) klass.fixture_paths # => ["test/fixtures"]Andrew Novoselac
-
Introduce
TestFixtures#fixture_paths.Multiple fixture paths can now be specified using the
#fixture_pathsaccessor. Apps will continue to havetest/fixturesas their one fixture path by default, but additional fixture paths can be specified.ActiveSupport::TestCase.fixture_paths << "component1/test/fixtures" ActiveSupport::TestCase.fixture_paths << "component2/test/fixtures"TestFixtures#fixture_pathis now deprecated.Andrew Novoselac
-
Adds support for deferrable exclude constraints in PostgreSQL.
By default, exclude constraints in PostgreSQL are checked after each statement. This works for most use cases, but becomes a major limitation when replacing records with overlapping ranges by using multiple statements.
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :immediatePassing
deferrable: :immediatechecks constraint after each statement, but allows manually deferring the check usingSET CONSTRAINTS ALL DEFERREDwithin a transaction. This will cause the excludes to be checked after the transaction.It's also possible to change the default behavior from an immediate check (after the statement), to a deferred check (after the transaction):
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :deferredHiroyuki Ishii
-
Respect
foreign_typeoption todelegated_typefor{role}_classmethod.Usage of
delegated_typewith non-conventional{role}_typecolumn names can now be specified withforeign_typeoption. This option is the same asforeign_typeas forwarded to the underlyingbelongs_toassociation thatdelegated_typewraps.Jason Karns
-
Add support for unique constraints (PostgreSQL-only).
add_unique_key :sections, [:position], deferrable: :deferred, name: "unique_section_position" remove_unique_key :sections, name: "unique_section_position"See PostgreSQL's Unique Constraints documentation for more on unique constraints.
By default, unique constraints in PostgreSQL are checked after each statement. This works for most use cases, but becomes a major limitation when replacing records with unique column by using multiple statements.
An example of swapping unique columns between records.
position is unique column
old_item = Item.create!(position: 1)
new_item = Item.create!(position: 2)
Item.transaction do
old_item.update!(position: 2)
new_item.update!(position: 1)
end
```
Using the default behavior, the transaction would fail when executing the
first `UPDATE` statement.
By passing the `:deferrable` option to the `add_unique_key` statement in
migrations, it's possible to defer this check.
```ruby
add_unique_key :items, [:position], deferrable: :immediate
```
Passing `deferrable: :immediate` does not change the behaviour of the previous example,
but allows manually deferring the check using `SET CONSTRAINTS ALL DEFERRED` within a transaction.
This will cause the unique constraints to be checked after the transaction.
It's also possible to adjust the default behavior from an immediate
check (after the statement), to a deferred check (after the transaction):
```ruby
add_unique_key :items, [:position], deferrable: :deferred
```
If you want to change an existing unique index to deferrable, you can use :using_index
to create deferrable unique constraints.
```ruby
add_unique_key :items, deferrable: :deferred, using_index: "index_items_on_position"
```
*Hiroyuki Ishii*
-
Remove deprecated
Tasks::DatabaseTasks.schema_file_type.Rafael Mendonça França
-
Remove deprecated
config.active_record.partial_writes.Rafael Mendonça França
-
Remove deprecated
ActiveRecord::Baseconfig accessors.Rafael Mendonça França
-
Remove the
:include_replicasargument fromconfigs_for. Use:include_hiddenargument instead.Eileen M. Uchitelle
-
Allow applications to lookup a config via a custom hash key.
If you have registered a custom config or want to find configs where the hash matches a specific key, now you can pass
config_keytoconfigs_for. For example if you have adb_configwith the keyvitessyou can look up a database configuration hash by matching that key.ActiveRecord::Base.configurations.configs_for(env_name: "development", name: "primary", config_key: :vitess) ActiveRecord::Base.configurations.configs_for(env_name: "development", config_key: :vitess)Eileen M. Uchitelle
-
Allow applications to register a custom database configuration handler.
Adds a mechanism for registering a custom handler for cases where you want database configurations to respond to custom methods. This is useful for non-Rails database adapters or tools like Vitess that you may want to configure differently from a standard
HashConfigorUrlConfig.Given the following database YAML we want the
animalsdb to create aCustomConfigobject instead while theprimarydatabase will be aUrlConfig:development: primary: url: postgres://localhost/primary animals: url: postgres://localhost/animals custom_config: sharded: 1To register a custom handler first make a class that has your custom methods:
class CustomConfig < ActiveRecord::DatabaseConfigurations::UrlConfig def sharded? custom_config.fetch("sharded", false) end private def custom_config configuration_hash.fetch(:custom_config) end endThen register the config in an initializer:
ActiveRecord::DatabaseConfigurations.register_db_config_handler do |env_name, name, url, config| next unless config.key?(:custom_config) CustomConfig.new(env_name, name, url, config) endWhen the application is booted, configuration hashes with the
:custom_configkey will beCustomConfigobjects and respond tosharded?. Applications must handle the condition in which Active Record should use their custom handler.Eileen M. Uchitelle and John Crepezzi
-
ActiveRecord::Base.serializeno longer uses YAML by default.YAML isn't particularly performant and can lead to security issues if not used carefully.
Unfortunately there isn't really any good serializers in Ruby's stdlib to replace it.
The obvious choice would be JSON, which is a fine format for this use case, however the JSON serializer in Ruby's stdlib isn't strict enough, as it fallback to casting unknown types to strings, which could lead to corrupted data.
Some third party JSON libraries like
Ojhave a suitable strict mode.So it's preferable that users choose a serializer based on their own constraints.
The original default can be restored by setting
config.active_record.default_column_serializer = YAML.Jean Boussier
-
ActiveRecord::Base.serializesignature changed.Rather than a single positional argument that accepts two possible types of values,
serializenow accepts two distinct keyword arguments.Before:
serialize :content, JSON serialize :backtrace, ArrayAfter:
serialize :content, coder: JSON serialize :backtrace, type: ArrayJean Boussier
-
YAML columns use
YAML.safe_dumpif available.As of
psych 5.1.0,YAML.safe_dumpcan now apply the same permitted types restrictions thanYAML.safe_load.It's preferable to ensure the payload only use allowed types when we first try to serialize it, otherwise you may end up with invalid records in the database.
Jean Boussier
-
ActiveRecord::QueryLogsbetter handle broken encoding.It's not uncommon when building queries with BLOB fields to contain binary data. Unless the call carefully encode the string in ASCII-8BIT it generally end up being encoded in
UTF-8, andQueryLogswould end up failing on it.ActiveRecord::QueryLogsno longer depend on the query to be properly encoded.Jean Boussier
-
Fix a bug where
ActiveRecord::Generators::ModelGeneratorwould not respect create_table_migration template overrides.rails g model create_books title:string content:textwill now read from the create_table_migration.rb.tt template in the following locations in order:
lib/templates/active_record/model/create_table_migration.rb lib/templates/active_record/migration/create_table_migration.rbSpencer Neste
-
ActiveRecord::Relation#explainnow accepts options.For databases and adapters which support them (currently PostgreSQL and MySQL), options can be passed to
explainto provide more detailed query plan analysis:Customer.where(id: 1).joins(:orders).explain(:analyze, :verbose)Reid Lynch
-
Multiple
Arel::Nodes::SqlLiteralnodes can now be added together to formArel::Nodes::Fragmentsnodes. This allows joining several pieces of SQL.Matthew Draper, Ole Friis
-
ActiveRecord::Base#signed_idraises if called on a new record.Previously it would return an ID that was not usable, since it was based on
id = nil.Alex Ghiculescu
-
Allow SQL warnings to be reported.
Active Record configs can be set to enable SQL warning reporting.
Configure action to take when SQL query produces warning
config.active_record.db_warnings_action = :raise
Configure allowlist of warnings that should always be ignored
config.active_record.db_warnings_ignore = [
/Invalid utf8mb4 character string/,
"An exact warning message",
]
```
This is supported for the MySQL and PostgreSQL adapters.
*Adrianna Chang*, *Paarth Madan*
-
Add
#regroupquery method as a short-hand for.unscope(:group).group(fields)Example:
Post.group(:title).regroup(:author)
SELECT posts.* FROM posts GROUP BY posts.author
```
*Danielius Visockas*
-
PostgreSQL adapter method
enable_extensionnow allows parameter to be[schema_name.]<extension_name>if the extension must be installed on another schema.Example:
enable_extension('heroku_ext.hstore')Leonardo Luarte
-
Add
:includeoption toadd_index.Add support for including non-key columns in indexes for PostgreSQL with the
INCLUDEparameter.add_index(:users, :email, include: [:id, :created_at])will result in:
CREATE INDEX index_users_on_email USING btree (email) INCLUDE (id, created_at)Steve Abrams
-
ActiveRecord::Relation’s#any?,#none?, and#one?methods take an optional pattern argument, more closely matching theirEnumerableequivalents.George Claghorn
-
Add
ActiveRecord::Base.normalizesfor declaring attribute normalizations.An attribute normalization is applied when the attribute is assigned or updated, and the normalized value will be persisted to the database. The normalization is also applied to the corresponding keyword argument of query methods, allowing records to be queried using unnormalized values.
For example:
class User < ActiveRecord::Base normalizes :email, with: -> email { email.strip.downcase } normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") } end user = User.create(email: " CRUISE-CONTROL@EXAMPLE.COM\n") user.email # => "cruise-control@example.com" user = User.find_by(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") user.email # => "cruise-control@example.com" user.email_before_type_cast # => "cruise-control@example.com" User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0 User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"Jonathan Hefner
-
Hide changes to before_committed! callback behaviour behind flag.
In #46525, behavior around before_committed! callbacks was changed so that callbacks would run on every enrolled record in a transaction, not just the first copy of a record. This change in behavior is now controlled by a configuration option,
config.active_record.before_committed_on_all_records. It will be enabled by default on Rails 7.1.Adrianna Chang
-
The
namespaced_controllerQuery Log tag now matches thecontrollerformatFor example, a request processed by
NameSpaced::UsersControllerwill now log as::controller # "users" :namespaced_controller # "name_spaced/users"Alex Ghiculescu
-
Return only unique ids from ActiveRecord::Calculations#ids
Updated ActiveRecord::Calculations#ids to only return the unique ids of the base model when using eager_load, preload and includes.
Post.find_by(id: 1).comments.count
=> 5
Post.includes(:comments).where(id: 1).pluck(:id)
=> [1, 1, 1, 1, 1]
Post.includes(:comments).where(id: 1).ids
=> [1]
```
*Joshua Young*
-
Stop using
LOWER()for case-insensitive queries oncitextcolumnsPreviously,
LOWER()was added for e.g. uniqueness validations withcase_sensitive: false. It wasn't mentioned in the documentation that the index withoutLOWER()wouldn't be used in this case.Phil Pirozhkov
-
Extract
#sync_timezone_changesmethod in AbstractMysqlAdapter to enable subclasses to sync database timezone changes without overriding#raw_execute.Adrianna Chang, Paarth Madan
-
Do not write additional new lines when dumping sql migration versions
This change updates the
insert_versions_sqlfunction so that the database insert string containing the current database migration versions does not end with two additional new lines.Misha Schwartz
-
Fix
composed_ofvalue freezing and duplication.Previously composite values exhibited two confusing behaviors:
- When reading a compositve value it'd NOT be frozen, allowing it to get out of sync with its underlying database columns.
- When writing a compositve value the argument would be frozen, potentially confusing the caller.
Currently, composite values instantiated based on database columns are frozen (addressing the first issue) and assigned compositve values are duplicated and the duplicate is frozen (addressing the second issue).
Greg Navis
-
Fix redundant updates to the column insensitivity cache
Fixed redundant queries checking column capability for insensitive comparison.
Phil Pirozhkov
-
Allow disabling methods generated by
ActiveRecord.enum.Alfred Dominic
-
Avoid validating
belongs_toassociation if it has not changed.Previously, when updating a record, Active Record will perform an extra query to check for the presence of
belongs_toassociations (if the presence is configured to be mandatory), even if that attribute hasn't changed.Currently, only
belongs_to-related columns are checked for presence. It is possible to have orphaned records with this approach. To avoid this problem, you need to use a foreign key.This behavior can be controlled by configuration:
config.active_record.belongs_to_required_validates_foreign_key = falseand will be disabled by default with
config.load_defaults 7.1.fatkodima
-
has_oneandbelongs_toassociations now define areset_associationmethod on the owner model (whereassociationis the name of the association). This method unloads the cached associate record, if any, and causes the next access to query it from the database.George Claghorn
-
Allow per attribute setting of YAML permitted classes (safe load) and unsafe load.
Carlos Palhares
-
Add a build persistence method
Provides a wrapper for
new, to provide feature parity withcreates ability to create multiple records from an array of hashes, using the same notation as thebuildmethod on associations.Sean Denny
-
Raise on assignment to readonly attributes
class Post < ActiveRecord::Base attr_readonly :content end Post.create!(content: "cannot be updated") post.content # "cannot be updated" post.content = "something else" # => ActiveRecord::ReadonlyAttributeErrorPreviously, assignment would succeed but silently not write to the database.
This behavior can be controlled by configuration:
config.active_record.raise_on_assign_to_attr_readonly = trueand will be enabled by default with
config.load_defaults 7.1.Alex Ghiculescu, Hartley McGuire
-
Allow unscoping of preload and eager_load associations
Added the ability to unscope preload and eager_load associations just like includes, joins, etc. See ActiveRecord::QueryMethods::VALID_UNSCOPING_VALUES for the full list of supported unscopable scopes.
query.unscope(:eager_load, :preload).group(:id).select(:id)David Morehouse
-
Add automatic filtering of encrypted attributes on inspect
This feature is enabled by default but can be disabled with
config.active_record.encryption.add_to_filter_parameters = falseHartley McGuire
-
Clear locking column on #dup
This change fixes not to duplicate locking_column like id and timestamps.
car = Car.create! car.touch car.lock_version #=> 1 car.dup.lock_version #=> 0Shouichi Kamiya, Seonggi Yang, Ryohei UEDA
-
Invalidate transaction as early as possible
After rescuing a
TransactionRollbackErrorexception Rails invalidates transactions earlier in the flow allowing the framework to skip issuing theROLLBACKstatement in more cases. Only affects adapters that havesavepoint_errors_invalidate_transactions?configured astrue, which at this point is only applicable to themysql2adapter.Nikita Vasilevsky
-
Allow configuring columns list to be used in SQL queries issued by an
ActiveRecord::BaseobjectIt is now possible to configure columns list that will be used to build an SQL query clauses when updating, deleting or reloading an
ActiveRecord::Baseobjectclass Developer < ActiveRecord::Base query_constraints :company_id, :id end developer = Developer.first.update(name: "Bob")
=> UPDATE "developers" SET "name" = 'Bob' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
```
*Nikita Vasilevsky*
-
Adds
validateto foreign keys and check constraints in schema.rbPreviously,
schema.rbwould not record ifvalidate: falsehad been used when adding a foreign key or check constraint, so restoring a database from the schema could result in foreign keys or check constraints being incorrectly validated.Tommy Graves
-
Adapter
#executemethods now accept anallow_retryoption. When set totrue, the SQL statement will be retried, up to the database's configuredconnection_retriesvalue, upon encountering connection-related errors.Adrianna Chang
-
Only trigger
after_commit :destroycallbacks when a database row is deleted.This prevents
after_commit :destroycallbacks from being triggered again whendestroyis called multiple times on the same record.Ben Sheldon
-
Fix
ciphertext_forfor yet-to-be-encrypted values.Previously,
ciphertext_forreturned the cleartext of values that had not yet been encrypted, such as with an unpersisted record:Post.encrypts :body post = Post.create!(body: "Hello") post.ciphertext_for(:body)
=> "{"p":"abc..."
post.body = "World"
post.ciphertext_for(:body)
=> "World"
```
Now, `ciphertext_for` will always return the ciphertext of encrypted
attributes:
```ruby
Post.encrypts :body
post = Post.create!(body: "Hello")
post.ciphertext_for(:body)
=> "{"p":"abc..."
post.body = "World"
post.ciphertext_for(:body)
=> "{"p":"xyz..."
```
*Jonathan Hefner*
-
Fix a bug where using groups and counts with long table names would return incorrect results.
Shota Toguchi, Yusaku Ono
-
Fix encryption of column default values.
Previously, encrypted attributes that used column default values appeared to be encrypted on create, but were not:
Book.encrypts :name book = Book.create! book.name
=> ""
book.name_before_type_cast
=> "{"p":"abc..."
book.reload.name_before_type_cast
=> ""
```
Now, attributes with column default values are encrypted:
```ruby
Book.encrypts :name
book = Book.create!
book.name
=> ""
book.name_before_type_cast
=> "{"p":"abc..."
book.reload.name_before_type_cast
=> "{"p":"abc..."
```
*Jonathan Hefner*
-
Deprecate delegation from
Basetoconnection_handler.Calling
Base.clear_all_connections!,Base.clear_active_connections!,Base.clear_reloadable_connections!andBase.flush_idle_connections!is deprecated. Please call these methods on the connection handler directly. In future Rails versions, the delegation fromBaseto theconnection_handlerwill be removed.Eileen M. Uchitelle
-
Allow ActiveRecord::QueryMethods#reselect to receive hash values, similar to ActiveRecord::QueryMethods#select
Sampat Badhe
-
Validate options when managing columns and tables in migrations.
If an invalid option is passed to a migration method like
create_tableandadd_column, an error will be raised instead of the option being silently ignored. Validation of the options will only be applied for new migrations that are created.Guo Xiang Tan, George Wambold
-
Update query log tags to use the SQLCommenter format by default. See #46179
To opt out of SQLCommenter-formatted query log tags, set
config.active_record.query_log_tags_format = :legacy. By default, this is set to:sqlcommenter.Modulitos and Iheanyi
-
Allow any ERB in the database.yml when creating rake tasks.
Any ERB can be used in
database.ymleven if it accesses environment configurations.Deprecates
config.active_record.suppress_multiple_database_warning.Eike Send
-
Add table to error for duplicate column definitions.
If a migration defines duplicate columns for a table, the error message shows which table it concerns.
Petrik de Heus
-
Fix erroneous nil default precision on virtual datetime columns.
Prior to this change, virtual datetime columns did not have the same default precision as regular datetime columns, resulting in the following being erroneously equivalent:
t.virtual :name, type: datetime, as: "expression" t.virtual :name, type: datetime, precision: nil, as: "expression"This change fixes the default precision lookup, so virtual and regular datetime column default precisions match.
Sam Bostock
-
Use connection from
#with_raw_connectionin#quote_string.This ensures that the string quoting is wrapped in the reconnect and retry logic that
#with_raw_connectionoffers.Adrianna Chang
-
Add
expires_atoption tosigned_id.Shouichi Kamiya
-
Allow applications to set retry deadline for query retries.
Building on the work done in #44576 and #44591, we extend the logic that automatically reconnects database connections to take into account a timeout limit. We won't retry a query if a given amount of time has elapsed since the query was first attempted. This value defaults to nil, meaning that all retryable queries are retried regardless of time elapsed, but this can be changed via the
retry_deadlineoption in the database config.Adrianna Chang
-
Fix a case where the query cache can return wrong values. See #46044
Aaron Patterson
-
Support MySQL's ssl-mode option for MySQLDatabaseTasks.
Verifying the identity of the database server requires setting the ssl-mode option to VERIFY_CA or VERIFY_IDENTITY. This option was previously ignored for MySQL database tasks like creating a database and dumping the structure.
Petrik de Heus
-
Move
ActiveRecord::InternalMetadatato an independent object.ActiveRecord::InternalMetadatano longer inherits fromActiveRecord::Baseand is now an independent object that should be instantiated with aconnection. This class is private and should not be used by applications directly. If you want to interact with the schema migrations table, please access it on the connection directly, for example:ActiveRecord::Base.connection.schema_migration.Eileen M. Uchitelle
-
Deprecate quoting
ActiveSupport::Durationas an integerUsing ActiveSupport::Duration as an interpolated bind parameter in a SQL string template is deprecated. To avoid this warning, you should explicitly convert the duration to a more specific database type. For example, if you want to use a duration as an integer number of seconds:
Record.where("duration = ?", 1.hour.to_i)If you want to use a duration as an ISO 8601 string:
Record.where("duration = ?", 1.hour.iso8601)Aram Greenman
-
Allow
QueryMethods#in_order_ofto order by a string column name.Post.in_order_of("id", [4,2,3,1]).to_a Post.joins(:author).in_order_of("authors.name", ["Bob", "Anna", "John"]).to_aIgor Kasyanchuk
-
Move
ActiveRecord::SchemaMigrationto an independent object.ActiveRecord::SchemaMigrationno longer inherits fromActiveRecord::Baseand is now an independent object that should be instantiated with aconnection. This class is private and should not be used by applications directly. If you want to interact with the schema migrations table, please access it on the connection directly, for example:ActiveRecord::Base.connection.schema_migration.Eileen M. Uchitelle
-
Deprecate
all_connection_poolsand makeconnection_pool_listmore explicit.Following on #45924
all_connection_poolsis now deprecated.connection_pool_listwill either take an explicit role or applications can opt into the new behavior by passing:all.Eileen M. Uchitelle
-
Fix connection handler methods to operate on all pools.
active_connections?,clear_active_connections!,clear_reloadable_connections!,clear_all_connections!, andflush_idle_connections!now operate on all pools by default. Previously they would default to using thecurrent_roleor:writingrole unless specified.Eileen M. Uchitelle
-
Allow ActiveRecord::QueryMethods#select to receive hash values.
Currently,
selectmight receive only raw sql and symbols to define columns and aliases to select.With this change we can provide
hashas argument, for example:Post.joins(:comments).select(posts: [:id, :title, :created_at], comments: [:id, :body, :author_id]) #=> "SELECT \"posts\".\"id\", \"posts\".\"title\", \"posts\".\"created_at\", \"comments\".\"id\", \"comments\".\"body\", \"comments\".\"author_id\"
FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id""
Post.joins(:comments).select(posts: { id: :post_id, title: :post_title }, comments: { id: :comment_id, body: :comment_body })
#=> "SELECT posts.id as post_id, posts.title as post_title, comments.id as comment_id, comments.body as comment_body
FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id""
```
*Oleksandr Holubenko*, *Josef Šimánek*, *Jean Boussier*
-
Adapts virtual attributes on
ActiveRecord::Persistence#becomes.When source and target classes have a different set of attributes adapts attributes such that the extra attributes from target are added.
class Person < ApplicationRecord end class WebUser < Person attribute :is_admin, :boolean after_initialize :set_admin def set_admin write_attribute(:is_admin, email =~ /@​ourcompany\.com$/) end end person = Person.find_by(email: "email@ourcompany.com") person.respond_to? :is_admin
=> false
person.becomes(WebUser).is_admin?
=> true
```
*Jacopo Beschi*, *Sampson Crowley*
-
Fix
ActiveRecord::QueryMethods#in_order_ofto includenils, to match the behavior ofEnumerable#in_order_of.For example,
Post.in_order_of(:title, [nil, "foo"])will now include posts withniltitles, the same asPost.all.to_a.in_order_of(:title, [nil, "foo"]).fatkodima
-
Optimize
add_timestampsto use a single SQL statement.add_timestamps :my_tableNow results in the following SQL:
ALTER TABLE "my_table" ADD COLUMN "created_at" datetime(6) NOT NULL, ADD COLUMN "updated_at" datetime(6) NOT NULLIliana Hadzhiatanasova
-
Add
drop_enummigration command for PostgreSQLThis does the inverse of
create_enum. Before dropping an enum, ensure you have dropped columns that depend on it.Alex Ghiculescu
-
Adds support for
if_existsoption when removing a check constraint.The
remove_check_constraintmethod now accepts anif_existsoption. If set to true an error won't be raised if the check constraint doesn't exist.Margaret Parsa and Aditya Bhutani
-
find_or_create_bynow try to find a second time if it hits a unicity constraint.find_or_create_byalways has been inherently racy, either creating multiple duplicate records or failing withActiveRecord::RecordNotUniquedepending on whether a proper unicity constraint was set.create_or_find_bywas introduced for this use case, however it's quite wasteful when the record is expected to exist most of the time, as INSERT require to send more data than SELECT and require more work from the database. Also on some databases it can actually consume a primary key increment which is undesirable.So for case where most of the time the record is expected to exist,
find_or_create_bycan be made race-condition free by re-trying thefindif thecreatefailed withActiveRecord::RecordNotUnique. This assumes that the table has the proper unicity constraints, if not,find_or_create_bywill still lead to duplicated records.Jean Boussier, Alex Kitchens
-
Introduce a simpler constructor API for ActiveRecord database adapters.
Previously the adapter had to know how to build a new raw connection to support reconnect, but also expected to be passed an initial already- established connection.
When manually creating an adapter instance, it will now accept a single config hash, and only establish the real connection on demand.
Matthew Draper
-
Avoid redundant
SELECT 1connection-validation query during DB pool checkout when possible.If the first query run during a request is known to be idempotent, it can be used directly to validate the connection, saving a network round-trip.
Matthew Draper
-
Automatically reconnect broken database connections when safe, even mid-request.
When an error occurs while attempting to run a known-idempotent query, and not inside a transaction, it is safe to immediately reconnect to the database server and try again, so this is now the default behavior.
This new default should always be safe -- to support that, it's consciously conservative about which queries are considered idempotent -- but if necessary it can be disabled by setting the
connection_retriesconnection option to0.Matthew Draper
-
Avoid removing a PostgreSQL extension when there are dependent objects.
Previously, removing an extension also implicitly removed dependent objects. Now, this will raise an error.
You can force removing the extension:
disable_extension :citext, force: :cascadeFixes #29091.
fatkodima
-
Allow nested functions as safe SQL string
Michael Siegfried
-
Allow
destroy_association_async_job=to be configured with a class string instead of a constant.Defers an autoloading dependency between
ActiveRecord::BaseandActiveJob::Baseand moves the configuration ofActiveRecord::DestroyAssociationAsyncJobfrom ActiveJob to ActiveRecord.Deprecates
ActiveRecord::ActiveJobRequiredErrorand now raises aNameErrorif the job class is unloadable or anActiveRecord::ConfigurationErrorifdependent: :destroy_asyncis declared on an association but there is no job class configured.Ben Sheldon
-
Fix
ActiveRecord::Storeto serialize as a regular HashPreviously it would serialize as an
ActiveSupport::HashWithIndifferentAccesswhich is wasteful and cause problem with YAML safe_load.Jean Boussier
-
Add
timestamptzas a time zone aware type for PostgreSQLThis is required for correctly parsing
timestamp with time zonevalues in your database.If you don't want this, you can opt out by adding this initializer:
ActiveRecord::Base.time_zone_aware_types -= [:timestamptz]Alex Ghiculescu
-
Add new
ActiveRecord::Base.generates_token_forAPI.Currently,
signed_idfulfills the role of generating tokens for e.g. resetting a password. However, signed IDs cannot reflect record state, so if a token is intended to be single-use, it must be tracked in a database at least until it expires.With
generates_token_for, a token can embed data from a record. When using the token to fetch the record, the data from the token and the current data from the record will be compared. If the two do not match, the token will be treated as invalid, the same as if it had expired. For example:class User < ActiveRecord::Base has_secure_password generates_token_for :password_reset, expires_in: 15.minutes do
A password's BCrypt salt changes when the password is updated.
By embedding (part of) the salt in a token, the token will
expire when the password is updated.
BCrypt::Password.new(password_digest).salt[-10..]
end
end
user = User.first
token = user.generate_token_for(:password_reset)
User.find_by_token_for(:password_reset, token) # => user
user.update!(password: "new password")
User.find_by_token_for(:password_reset, token) # => nil
```
*Jonathan Hefner*
-
Optimize Active Record batching for whole table iterations.
Previously,
in_batchesgot all the ids and constructed anIN-based query for each batch. When iterating over the whole tables, this approach is not optimal as it loads unneeded ids andINqueries with lots of items are slow.Now, whole table iterations use range iteration (
id >= x AND id <= y) by default which can make iteration several times faster. E.g., tested on a PostgreSQL table with 10 million records: querying (253svs30s), updating (288svs124s), deleting (268svs83s).Only whole table iterations use this style of iteration by default. You can disable this behavior by passing
use_ranges: false. If you iterate over the table and the only condition is, e.g.,archived_at: nil(and only a tiny fraction of the records are archived), it makes sense to opt in to this approach:Project.where(archived_at: nil).in_batches(use_ranges: true) do |relation|
do something
end
```
See #​45414 for more details.
*fatkodima*
-
.withquery method added. Construct common table expressions with ease and getActiveRecord::Relationback.Post.with(posts_with_comments: Post.where("comments_count > ?", 0))
=> ActiveRecord::Relation
WITH posts_with_comments AS (SELECT * FROM posts WHERE (comments_count > 0)) SELECT * FROM posts
```
*Vlado Cingel*
-
Don't establish a new connection if an identical pool exists already.
Previously, if
establish_connectionwas called on a class that already had an established connection, the existing connection would be removed regardless of whether it was the same config. Now if a pool is found with the same values as the new connection, the existing connection will be returned instead of creating a new one.This has a slight change in behavior if application code is depending on a new connection being established regardless of whether it's identical to an existing connection. If the old behavior is desirable, applications should call
ActiveRecord::Base#remove_connectionbefore establishing a new one. Callingestablish_connectionwith a different config works the same way as it did previously.Eileen M. Uchitelle
-
Update
db:preparetask to load schema when an uninitialized database exists, and dump schema after migrations.Ben Sheldon
-
Fix supporting timezone awareness for
tsrangeandtstzrangearray columns.
In database migrations
add_column :shops, :open_hours, :tsrange, array: true
In app config
ActiveRecord::Base.time_zone_aware_types += [:tsrange]
In the code times are properly converted to app time zone
Shop.create!(open_hours: [Time.current..8.hour.from_now])
```
*Wojciech Wnętrzak*
-
Introduce strategy pattern for executing migrations.
By default, migrations will use a strategy object that delegates the method to the connection adapter. Consumers can implement custom strategy objects to change how their migrations run.
Adrianna Chang
-
Add adapter option disallowing foreign keys
This adds a new option to be added to
database.ymlwhich enables skipping foreign key constraints usage even if the underlying database supports them.Usage:
development: <<: *default database: storage/development.sqlite3 foreign_keys: falsePaulo Barros
-
Add configurable deprecation warning for singular associations
This adds a deprecation warning when using the plural name of a singular associations in
where. It is possible to opt into the new more performant behavior withconfig.active_record.allow_deprecated_singular_associations_name = falseAdam Hess
-
Run transactional callbacks on the freshest instance to save a given record within a transaction.
When multiple Active Record instances change the same record within a transaction, Rails runs
after_commitorafter_rollbackcallbacks for only one of them.config.active_record.run_commit_callbacks_on_first_saved_instances_in_transactionwas added to specify how Rails chooses which instance receives the callbacks. The framework defaults were changed to use the new logic.When
config.active_record.run_commit_callbacks_on_first_saved_instances_in_transactionistrue, transactional callbacks are run on the first instance to save, even though its instance state may be stale.When it is
false, which is the new framework default starting with version 7.1, transactional callbacks are run on the instances with the freshest instance state. Those instances are chosen as follows:- In general, run transactional callbacks on the last instance to save a given record within the transaction.
- There are two exceptions:
- If the record is created within the transaction, then updated by
another instance,
after_create_commitcallbacks will be run on the second instance. This is instead of theafter_update_commitcallbacks that would naively be run based on that instance’s state. - If the record is destroyed within the transaction, then
after_destroy_commitcallbacks will be fired on the last destroyed instance, even if a stale instance subsequently performed an update (which will have affected 0 rows).
- If the record is created within the transaction, then updated by
another instance,
Cameron Bothner and Mitch Vollebregt
-
Enable strict strings mode for
SQLite3Adapter.Configures SQLite with a strict strings mode, which disables double-quoted string literals.
SQLite has some quirks around double-quoted string literals. It first tries to consider double-quoted strings as identifier names, but if they don't exist it then considers them as string literals. Because of this, typos can silently go unnoticed. For example, it is possible to create an index for a non existing column. See SQLite documentation for more details.
If you don't want this behavior, you can disable it via:
config/application.rb
config.active_record.sqlite3_adapter_strict_strings_by_default = false
```
Fixes #​27782.
*fatkodima*, *Jean Boussier*
-
Resolve issue where a relation cache_version could be left stale.
Previously, when
resetwas called on a relation object it did not reset the cache_versions ivar. This led to a confusing situation where despite having the correct data the relation still reported a stale cache_version.Usage:
developers = Developer.all developers.cache_version Developer.update_all(updated_at: Time.now.utc + 1.second) developers.cache_version # Stale cache_version developers.reset developers.cache_version # Returns the current correct cache_versionFixes #45341.
Austen Madden
-
Add support for exclusion constraints (PostgreSQL-only).
add_exclusion_constraint :invoices, "daterange(start_date, end_date) WITH &&", using: :gist, name: "invoices_date_overlap" remove_exclusion_constraint :invoices, name: "invoices_date_overlap"See PostgreSQL's
CREATE TABLE ... EXCLUDE ...documentation for more on exclusion constraints.Alex Robbin
-
change_column_nullraises if a non-boolean argument is providedPreviously if you provided a non-boolean argument,
change_column_nullwould treat it as truthy and make your column nullable. This could be surprising, so now the input must be eithertrueorfalse.change_column_null :table, :column, true # good change_column_null :table, :column, false # good change_column_null :table, :column, from: true, to: false # raises (previously this made the column nullable)Alex Ghiculescu
-
Enforce limit on table names length.
Fixes #45130.
fatkodima
-
Adjust the minimum MariaDB version for check constraints support.
Eddie Lebow
-
Fix Hstore deserialize regression.
edsharp
-
Add validity for PostgreSQL indexes.
connection.index_exists?(:users, :email, valid: true) connection.indexes(:users).select(&:valid?)fatkodima
-
Fix eager loading for models without primary keys.
Anmol Chopra, Matt Lawrence, and Jonathan Hefner
-
Avoid validating a unique field if it has not changed and is backed by a unique index.
Previously, when saving a record, Active Record will perform an extra query to check for the uniqueness of each attribute having a
uniquenessvalidation, even if that attribute hasn't changed. If the database has the corresponding unique index, then this validation can never fail for persisted records, and we could safely skip it.fatkodima
-
Stop setting
sql_auto_is_nullSince version 5.5 the default has been off, we no longer have to manually turn it off.
Adam Hess
-
Fix
touchto raise an error for readonly columns.fatkodima
-
Add ability to ignore tables by regexp for SQL schema dumps.
ActiveRecord::SchemaDumper.ignore_tables = [/^_/]fatkodima
-
Avoid queries when performing calculations on contradictory relations.
Previously calculations would make a query even when passed a contradiction, such as
User.where(id: []).count. We no longer perform a query in that scenario.This applies to the following calculations:
count,sum,average,minimumandmaximumLuan Vieira, John Hawthorn and Daniel Colson
-
Allow using aliased attributes with
insert_all/upsert_all.class Book < ApplicationRecord alias_attribute :title, :name end Book.insert_all [{ title: "Remote", author_id: 1 }], returning: :titlefatkodima
-
Support encrypted attributes on columns with default db values.
This adds support for encrypted attributes defined on columns with default values. It will encrypt those values at creation time. Before, it would raise an error unless
config.active_record.encryption.support_unencrypted_datawas true.Jorge Manrubia and Dima Fatko
-
Allow overriding
reading_request?inDatabaseSelector::ResolverThe default implementation checks if a request is a
get?orhead?, but you can now change it to anything you like. If the method returns true,Resolver#readgets called meaning the request could be served by the replica database.Alex Ghiculescu
-
Remove
ActiveRecord.legacy_connection_handling.Eileen M. Uchitelle
-
rails db:schema:{dump,load}now checksENV["SCHEMA_FORMAT"]before configSince
rails db:structure:{dump,load}was deprecated there wasn't a simple way to dump a schema to both SQL and Ruby formats. You can now do this with an environment variable. For example:SCHEMA_FORMAT=sql rake db:schema:dumpAlex Ghiculescu
-
Fixed MariaDB default function support.
Defaults would be written wrong in "db/schema.rb" and not work correctly if using
db:schema:load. Further more the function name would be added as string content when saving new records.kaspernj
-
Add
active_record.destroy_association_async_batch_sizeconfigurationThis allows applications to specify the maximum number of records that will be destroyed in a single background job by the
dependent: :destroy_asyncassociation option. By default, the current behavior will remain the same: when a parent record is destroyed, all dependent records will be destroyed in a single background job. If the number of dependent records is greater than this configuration, the records will be destroyed in multiple background jobs.Nick Holden
-
Fix
remove_foreign_keywith:if_existsoption when foreign key actually exists.fatkodima
-
Remove
--no-commentsflag in structure dumps for PostgreSQLThis broke some apps that used custom schema comments. If you don't want comments in your structure dump, you can use:
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ['--no-comments']Alex Ghiculescu
-
Reduce the memory footprint of fixtures accessors.
Until now fixtures accessors were eagerly defined using
define_method. So the memory usage was directly dependent of the number of fixtures and test suites.Instead fixtures accessors are now implemented with
method_missing, so they incur much less memory and CPU overhead.Jean Boussier
-
Fix
config.active_record.destroy_association_async_jobconfigurationconfig.active_record.destroy_association_async_jobshould allow applications to specify the job that will be used to destroy associated records in the background forhas_manyassociations with thedependent: :destroy_asyncoption. Previously, that was ignored, which meant the defaultActiveRecord::DestroyAssociationAsyncJobalways destroyed records in the background.Nick Holden
-
Fix
change_column_commentto preserve column's AUTO_INCREMENT in the MySQL adapterfatkodima
-
Fix quoting of
ActiveSupport::DurationandRationalnumbers in the MySQL adapter.Kevin McPhillips
-
Allow column name with COLLATE (e.g., title COLLATE "C") as safe SQL string
Shugo Maeda
-
Permit underscores in the VERSION argument to database rake tasks.
Eddie Lebow
-
Reversed the order of
INSERTstatements instructure.sqldumpsThis should decrease the likelihood of merge conflicts. New migrations will now be added at the top of the list.
For existing apps, there will be a large diff the next time
structure.sqlis generated.Alex Ghiculescu, Matt Larraz
-
Fix PG.connect keyword arguments deprecation warning on ruby 2.7
Fixes #44307.
Nikita Vasilevsky
-
Fix dropping DB connections after serialization failures and deadlocks.
Prior to 6.1.4, serialization failures and deadlocks caused rollbacks to be issued for both real transactions and savepoints. This breaks MySQL which disallows rollbacks of savepoints following a deadlock.
6.1.4 removed these rollbacks, for both transactions and savepoints, causing the DB connection to be left in an unknown state and thus discarded.
These rollbacks are now restored, except for savepoints on MySQL.
Thomas Morgan
-
Make
ActiveRecord::ConnectionPoolFiber-safeWhen
ActiveSupport::IsolatedExecutionState.isolation_levelis set to:fiber, the connection pool now supports multiple Fibers from the same Thread checking out connections from the pool.Alex Matchneer
-
Add
update_attribute!toActiveRecord::PersistenceSimilar to
update_attribute, but raisesActiveRecord::RecordNotSavedwhen abefore_*callback throws:abort.class Topic < ActiveRecord::Base before_save :check_title def check_title throw(:abort) if title == "abort" end end topic = Topic.create(title: "Test Title")
#=> #<Topic title: "Test Title">
topic.update_attribute!(:title, "Another Title")
#=> #<Topic title: "Another Title">
topic.update_attribute!(:title, "abort")
raises ActiveRecord::RecordNotSaved
```
*Drew Tempelmeyer*
-
Avoid loading every record in
ActiveRecord::Relation#pretty_print
Before
pp Foo.all # Loads the whole table.
After
pp Foo.all # Shows 10 items and an ellipsis.
```
*Ulysse Buonomo*
-
Change
QueryMethods#in_order_ofto drop records not listed in values.in_order_ofnow filters down to the values provided, to match the behavior of theEnumerableversion.Kevin Newton
-
Allow named expression indexes to be revertible.
Previously, the following code would raise an error in a reversible migration executed while rolling back, due to the index name not being used in the index removal.
add_index(:settings, "(data->'property')", using: :gin, name: :index_settings_data_property)Fixes #43331.
Oliver Günther
-
Fix incorrect argument in PostgreSQL structure dump tasks.
Updating the
--no-commentargument added in Rails 7 to the correct--no-commentsargument.Alex Dent
-
Fix migration compatibility to create SQLite references/belongs_to column as integer when migration version is 6.0.
Reference/belongs_to in migrations with version 6.0 were creating columns as bigint instead of integer for the SQLite Adapter.
Marcelo Lauxen
-
Fix
QueryMethods#in_order_ofto handle empty order list.Post.in_order_of(:id, []).to_aAlso more explicitly set the column as secondary order, so that any other value is still ordered.
Jean Boussier
-
Fix quoting of column aliases generated by calculation methods.
Since the alias is derived from the table name, we can't assume the result is a valid identifier.
class Test < ActiveRecord::Base self.table_name = '1abc' end Test.group(:id).count
syntax error at or near "1" (ActiveRecord::StatementInvalid)
LINE 1: SELECT COUNT(*) AS count_all, "1abc"."id" AS 1abc_id FROM "1...
```
*Jean Boussier*
-
Add
authenticate_bywhen usinghas_secure_password.authenticate_byis intended to replace code like the following, which returns early when a user with a matching email is not found:User.find_by(email: "...")&.authenticate("...")Such code is vulnerable to timing-based enumeration attacks, wherein an attacker can determine if a user account with a given email exists. After confirming that an account exists, the attacker can try passwords associated with that email address from other leaked databases, in case the user re-used a password across multiple sites (a common practice). Additionally, knowing an account email address allows the attacker to attempt a targeted phishing ("spear phishing") attack.
authenticate_byaddresses the vulnerability by taking the same amount of time regardless of whether a user with a matching email is found:User.authenticate_by(email: "...", password: "...")Jonathan Hefner
Action View
-
Introduce
ActionView::TestCase.register_parserregister_parser :rss, -> rendered { RSS::Parser.parse(rendered) } test "renders RSS" do article = Article.create!(title: "Hello, world") render formats: :rss, partial: article assert_equal "Hello, world", rendered.rss.items.last.title endBy default, register parsers for
:htmland:json.Sean Doyle
-
Fix
simple_formatwith blankwrapper_tagoption returns plain html tagBy default
simple_formatmethod returns the text wrapped with<p>. But if we explicitly specify thewrapper_tag: nilin the options, it returns the text wrapped with<></>tag.Before:
simple_format("Hello World", {}, { wrapper_tag: nil })
<>Hello World</>
```
After:
```ruby
simple_format("Hello World", {}, { wrapper_tag: nil })
Hello World
`
Configuration
-
If you want to rebase/retry this MR, check this box
This MR has been generated by Renovate Bot.