2022-03-03 03:47:59 +08:00
* Add `timestamptz` as a time zone aware type for PostgreSQL
This is required for correctly parsing `timestamp with time zone` values in your database.
If you don't want this, you can opt out by adding this initializer:
```ruby
ActiveRecord::Base.time_zone_aware_types -= [:timestamptz]
```
*Alex Ghiculescu*
2022-07-09 03:54:42 +08:00
* Add new `ActiveRecord::Base::generates_token_for` API.
Currently, `signed_id` fulfills 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 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:
```ruby
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
```
2022-07-11 04:33:50 +08:00
*Jonathan Hefner*
2022-07-09 03:54:42 +08:00
2022-06-21 07:13:38 +08:00
* Optimize Active Record batching for whole table iterations.
Previously, `in_batches` got all the ids and constructed an `IN` -based query for each batch.
When iterating over the whole tables, this approach is not optimal as it loads unneeded ids and
`IN` queries 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 (`253s` vs `30s` ),
updating (`288s` vs `124s` ), deleting (`268s` vs `83s` ).
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:
```ruby
Project.where(archived_at: nil).in_batches(use_ranges: true) do |relation|
# do something
end
```
See #45414 for more details.
*fatkodima*
2019-12-12 15:01:38 +08:00
* `.with` query method added. Construct common table expressions with ease and get `ActiveRecord::Relation` back.
```ruby
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*
Only remove connection for an existing pool if the config is different
Previously Rails would always remove the connection if it found a
matching class in the pool manager. Therefore if
`ActiveRecord::Base.establish_connection` was called with the same
config, each time it was called it would be clobbered, even though the
config hasn't changed and the existing connection is prefectly fine. As
far as I can tell from conversations and reading the history this
functionality was added for ActiveRecord tests to be able to clobber the
connection and use a new config, then re-establish the old connection.
Essentially outside Rake tasks and AR tests, this functionality doesn't
have a ton of value.
On top of not adding a ton of value, this has resulted in a few bugs. In
Rails 6.0 I made it so that if you established a connection on
`ApplicationRecord` Rails would treat that connection the same as
`ActiveRecord::Base.` The reason for this is that the Railtie
establishes a connection on boot to the first database, but then if
you're using multiple databases you're calling `connects_to` in your
`ApplicationRecord` or primary abstract class which essentially doubles
your connections to the same database. To avoid opening 2 connections to
the same database, Rails treats them the same.
However, because we have this code that removes existing connections,
when an application boots, `ApplicationRecord` will clobber the
connection that the Railtie established even though the connection
configs are the same.
This removal of the connection caused bugs in migrations that load up a
model connected to `ApplicationRecord` (ex `Post.first`) and then calls
`execute("SELECT 1")` (obviously a simplified example). When `execute`
runs the connection is different from the one opened to run the
migration and essentially it is lost when the `remove_connection` code
is called.
To fix this I've updated the code to only remove the connection if the
database config is different. Ultimately I'd like to remove this code
altogether but to do that we first need to stop using
`Base.establish_connection` in the rake tasks and tests. This will fix
the major bugs until I come up with a solution for the areas that
currently need to call `establish_connection` on Base.
The added benefit of this change is that if your app is calling
`establish_connection` multiple times with the same config, it is now
3x faster than the previous implementation because we can return the
found pool instead of setting it up again. To benchmark this I
duplicated the `establish_connection` method to use the new behavior
with a new name.
Benchmark script:
```ruby
require "active_record"
require "logger"
require "benchmark/ips"
config_hash = { "development" => { "primary" => { "adapter" => "mysql2", "username" => "rails", "database" => "activerecord_unittest"}}}
ActiveRecord::Base.configurations = config_hash
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "development", name: "primary")
p "Same model same config"
ActiveRecord::Base.connected_to(role: :writing, prevent_writes: true) do
Benchmark.ips do |x|
x.report "establish_connection with remove" do
ActiveRecord::Base.establish_connection(db_config)
end
x.report "establish_connection without remove" do
ActiveRecord::Base.establish_connection_no_remove(db_config)
end
x.compare!
end
end
```
Benchmark results:
```
Warming up --------------------------------------
establish_connection with remove
4.677k i/100ms
establish_connection without remove
19.501k i/100ms
Calculating -------------------------------------
establish_connection with remove
41.252k (±11.3%) i/s - 205.788k in 5.075525s
establish_connection without remove
179.205k (± 6.9%) i/s - 897.046k in 5.029742s
Comparison:
establish_connection without remove: 179205.1 i/s
establish_connection with remove: 41252.3 i/s - 4.34x (± 0.00) slower
```
Other changes:
1) sqlite3 now disconnects and reconnects the connection when `purge` is
called. This is necessary now that a new connection isn't created
everyt time `establish_connection` is called. Without this change to
purge the new database is left in an inaccessible state causing a
readonly error from the sqlite3 client. This wasn't happening in mysql
or postgres because they were already reconnecting the db connection.
2) I added `remove_connection` to tests that use `ApplicationRecord`.
This is required because `ApplicationRecord` or any class that is a
`primary_abstract_class` will be treated the same as
`ActiveRecord::Base`. This is fine in applications because they are
shared connections, but in the AR test environment, we don't want those
connnections to stick around (we want AR::Base back).
3) In the async tests I removed 2 calls to `establish_connection`. These
were causing sqlite3 tests to leak the state of async_executor because
it's stored on the connection. I'm not sure why these were calling
`establish_connection` but it's not necessary and was leaking state when
now that we are no longer removing the connection.
Fixes: #41855
Fixes: #41876
Fixes: #42873
Fixes: #43004
2022-06-24 03:15:40 +08:00
* Don't establish a new connection if an identical pool exists already.
Previously, if `establish_connection` was 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_connection` before establishing a new one. Calling `establish_connection` with a different config works the same way as it did previously.
*Eileen M. Uchitelle*
2022-06-26 23:23:00 +08:00
* Update `db:prepare` task to load schema when an uninitialized database exists, and dump schema after migrations.
*Ben Sheldon*
2022-06-14 16:35:50 +08:00
* Fix supporting timezone awareness for `tsrange` and `tstzrange` array columns.
```ruby
# 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*
2022-06-11 01:22:21 +08:00
* 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*
2022-06-20 17:11:29 +08:00
* Add adapter option disallowing foreign keys
This adds a new option to be added to `database.yml` which enables skipping
foreign key constraints usage even if the underlying database supports them.
Usage:
```yaml
development:
< < : * default
database: db/development.sqlite3
foreign_keys: false
```
*Paulo Barros*
2022-06-14 02:42:33 +08:00
* 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 with `config.active_record.allow_deprecated_singular_associations_name = false`
*Adam Hess*
2022-06-26 16:10:31 +08:00
* Run transactional callbacks on the freshest instance to save a given
2022-06-03 00:11:02 +08:00
record within a transaction.
2022-06-26 16:10:31 +08:00
When multiple Active Record instances change the same record within a
transaction, Rails runs `after_commit` or `after_rollback` callbacks for
only one of them. `config.active_record.run_commit_callbacks_on_first_saved_instances_in_transaction`
was added to specify how Rails chooses which instance receives the
2022-06-03 00:11:02 +08:00
callbacks. The framework defaults were changed to use the new logic.
2022-06-26 16:10:31 +08:00
When `config.active_record.run_commit_callbacks_on_first_saved_instances_in_transaction`
is `true` , transactional callbacks are run on the first instance to save,
2022-06-03 00:11:02 +08:00
even though its instance state may be stale.
2022-06-26 16:10:31 +08:00
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
2022-06-03 00:11:02 +08:00
instance state. Those instances are chosen as follows:
2022-06-26 16:10:31 +08:00
- In general, run transactional callbacks on the last instance to save a
2022-06-03 00:11:02 +08:00
given record within the transaction.
- There are two exceptions:
2022-06-26 16:10:31 +08:00
- If the record is created within the transaction, then updated by
another instance, `after_create_commit` callbacks will be run on the
second instance. This is instead of the `after_update_commit`
2022-06-03 00:11:02 +08:00
callbacks that would naively be run based on that instance’ s state.
2022-06-26 16:10:31 +08:00
- If the record is destroyed within the transaction, then
`after_destroy_commit` callbacks will be fired on the last destroyed
instance, even if a stale instance subsequently performed an update
2022-06-03 00:11:02 +08:00
(which will have affected 0 rows).
*Cameron Bothner and Mitch Vollebregt*
2022-06-14 07:40:03 +08:00
* 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 ](https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted ) for more details.
If you don't want this behavior, you can disable it via:
```ruby
# config/application.rb
config.active_record.sqlite3_adapter_strict_strings_by_default = false
```
Fixes #27782 .
*fatkodima* , *Jean Boussier*
2022-06-13 23:30:52 +08:00
* Resolve issue where a relation cache_version could be left stale.
Previously, when `reset` was 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:
```ruby
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_version
```
Fixes #45341 .
*Austen Madden*
2020-09-13 10:49:27 +08:00
* Add support for exclusion constraints (PostgreSQL-only).
```ruby
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 ...` ](https://www.postgresql.org/docs/12/sql-createtable.html#SQL-CREATETABLE-EXCLUDE ) documentation for more on exclusion constraints.
*Alex Robbin*
`change_column_null` should raise if a non-boolean 3rd argument is provided
Currently if you provide a non-boolean argument, `change_column_null` will treat it as truthy and make your column nullable. This might not be what you want. For example, I've noticed this happen a few times, where someone assumes that `change_column_null` and `change_column_default` work the same:
```ruby
change_column_default(:posts, :state, from: nil, to: "draft")
change_column_null(:posts, :state, from: true, to: false)
```
Reading the migration you would expect that the default is now "draft" and the column doesn't accept nulls. But actually, the "null" argument is `{ from: true, to: false }` which is truthy, so now the column accepts nulls. If you aren't paying close attention to the migration output you might miss this.
I think we can protect users here by having `change_column_null` require the 3rd argument to be `true` or `false` and raising an error otherwise. This PR implements that, for migrations created on Rails 7.1 onward.
2022-06-01 02:59:52 +08:00
* `change_column_null` raises if a non-boolean argument is provided
Previously if you provided a non-boolean argument, `change_column_null` would
treat it as truthy and make your column nullable. This could be surprising, so now
the input must be either `true` or `false` .
```ruby
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*
2022-05-20 01:54:52 +08:00
* Enforce limit on table names length.
Fixes #45130 .
*fatkodima*
2022-06-11 05:34:09 +08:00
* Adjust the minimum MariaDB version for check constraints support.
*Eddie Lebow*
2022-05-31 16:03:19 +08:00
* Fix Hstore deserialize regression.
*edsharp*
2022-05-24 00:20:03 +08:00
* Add validity for PostgreSQL indexes.
```ruby
connection.index_exists?(:users, :email, valid: true)
connection.indexes(:users).select(& :valid?)
```
*fatkodima*
2021-10-08 07:36:44 +08:00
* Fix eager loading for models without primary keys.
*Anmol Chopra* , *Matt Lawrence* , and *Jonathan Hefner*
2022-05-22 07:41:05 +08:00
* Avoid validating a unique field if it has not changed and is backed by a unique index.
2022-06-30 06:14:48 +08:00
Previously, when saving a record, Active Record will perform an extra query to check for the
uniqueness of each attribute having a `uniqueness` validation, 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.
2022-05-22 07:41:05 +08:00
*fatkodima*
2022-03-22 02:29:25 +08:00
* Stop setting `sql_auto_is_null`
Since version 5.5 the default has been off, we no longer have to manually turn it off.
*Adam Hess*
2022-05-18 21:03:45 +08:00
* Fix `touch` to raise an error for readonly columns.
*fatkodima*
2022-05-14 07:11:47 +08:00
* Add ability to ignore tables by regexp for SQL schema dumps.
```ruby
ActiveRecord::SchemaDumper.ignore_tables = [/^_/]
```
*fatkodima*
2022-05-06 01:56:36 +08:00
* 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` ,
`minimum` and `maximum`
*Luan Vieira, John Hawthorn and Daniel Colson*
2022-05-07 17:33:53 +08:00
* Allow using aliased attributes with `insert_all` /`upsert_all`.
```ruby
class Book < ApplicationRecord
alias_attribute :title, :name
end
Book.insert_all [{ title: "Remote", author_id: 1 }], returning: :title
```
*fatkodima*
2022-05-06 20:31:10 +08:00
* Support encrypted attributes on columns with default db values.
2022-03-22 02:29:25 +08:00
This adds support for encrypted attributes defined on columns with default values.
2022-05-15 01:41:01 +08:00
It will encrypt those values at creation time. Before, it would raise an
error unless `config.active_record.encryption.support_unencrypted_data` was true.
2022-05-06 20:31:10 +08:00
2022-05-15 01:41:01 +08:00
*Jorge Manrubia* and *Dima Fatko*
2022-05-06 20:31:10 +08:00
2022-04-23 09:55:09 +08:00
* Allow overriding `reading_request?` in `DatabaseSelector::Resolver`
The default implementation checks if a request is a `get?` or `head?` ,
but you can now change it to anything you like. If the method returns true,
`Resolver#read` gets called meaning the request could be served by the
replica database.
*Alex Ghiculescu*
2022-04-02 03:15:32 +08:00
* Remove `ActiveRecord.legacy_connection_handling` .
*Eileen M. Uchitelle*
2022-04-04 07:30:52 +08:00
* `rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
Since `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:dump
```
*Alex Ghiculescu*
2022-03-10 16:32:43 +08:00
* 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*
2022-02-05 05:32:24 +08:00
* Add `active_record.destroy_association_async_batch_size` configuration
This allows applications to specify the maximum number of records that will
be destroyed in a single background job by the `dependent: :destroy_async`
association 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*
2022-03-08 19:31:09 +08:00
* Fix `remove_foreign_key` with `:if_exists` option when foreign key actually exists.
*fatkodima*
2022-03-08 02:40:12 +08:00
* Remove `--no-comments` flag in structure dumps for PostgreSQL
This broke some apps that used custom schema comments. If you don't want
comments in your structure dump, you can use:
```ruby
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ['--no-comments']
```
*Alex Ghiculescu*
2022-02-23 21:31:40 +08:00
* 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*
2022-02-02 09:03:55 +08:00
* Fix `config.active_record.destroy_association_async_job` configuration
`config.active_record.destroy_association_async_job` should allow
applications to specify the job that will be used to destroy associated
records in the background for `has_many` associations with the
`dependent: :destroy_async` option. Previously, that was ignored, which
meant the default `ActiveRecord::DestroyAssociationAsyncJob` always
destroyed records in the background.
*Nick Holden*
2022-03-01 01:00:40 +08:00
* Fix `change_column_comment` to preserve column's AUTO_INCREMENT in the MySQL adapter
2022-02-19 01:59:06 +08:00
*fatkodima*
2022-02-12 02:04:02 +08:00
* Fix quoting of `ActiveSupport::Duration` and `Rational` numbers in the MySQL adapter.
*Kevin McPhillips*
2022-02-10 11:53:46 +08:00
* Allow column name with COLLATE (e.g., title COLLATE "C") as safe SQL string
*Shugo Maeda*
2022-01-27 04:36:01 +08:00
* Permit underscores in the VERSION argument to database rake tasks.
*Eddie Lebow*
2022-02-09 03:14:38 +08:00
* Reversed the order of `INSERT` statements in `structure.sql` dumps
This 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.sql`
is generated.
*Alex Ghiculescu* , *Matt Larraz*
2022-02-07 01:22:33 +08:00
* Fix PG.connect keyword arguments deprecation warning on ruby 2.7
Fixes #44307 .
*Nikita Vasilevsky*
2022-01-09 09:09:52 +08:00
* 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*
2022-01-09 02:11:05 +08:00
* Make `ActiveRecord::ConnectionPool` Fiber-safe
When `ActiveSupport::IsolatedExecutionState.isolation_level` is set to `:fiber` ,
the connection pool now supports multiple Fibers from the same Thread checking
out connections from the pool.
*Alex Matchneer*
2022-01-11 05:53:27 +08:00
* Add `update_attribute!` to `ActiveRecord::Persistence`
Similar to `update_attribute` , but raises `ActiveRecord::RecordNotSaved` when a `before_*` callback throws `:abort` .
```ruby
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*
2022-01-11 01:25:36 +08:00
* Avoid loading every record in `ActiveRecord::Relation#pretty_print`
2021-09-24 15:46:54 +08:00
```ruby
# Before
pp Foo.all # Loads the whole table.
# After
pp Foo.all # Shows 10 items and an ellipsis.
```
*Ulysse Buonomo*
2022-01-07 01:54:41 +08:00
* Change `QueryMethods#in_order_of` to drop records not listed in values.
`in_order_of` now filters down to the values provided, to match the behavior of the `Enumerable` version.
*Kevin Newton*
2022-01-02 04:25:28 +08:00
* 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.
```ruby
add_index(:settings, "(data->'property')", using: :gin, name: :index_settings_data_property)
```
Fixes #43331 .
*Oliver Günther*
2021-12-30 11:52:54 +08:00
* Fix incorrect argument in PostgreSQL structure dump tasks.
Updating the `--no-comment` argument added in Rails 7 to the correct `--no-comments` argument.
*Alex Dent*
2021-09-24 01:34:23 +08:00
* 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*
2021-09-12 08:08:22 +08:00
* Add a deprecation warning when `prepared_statements` configuration is not
set for the mysql2 adapter.
*Thiago Araujo and Stefanni Brasil*
2021-12-17 21:25:48 +08:00
* Fix `QueryMethods#in_order_of` to handle empty order list.
```ruby
Post.in_order_of(:id, []).to_a
```
Also more explicitly set the column as secondary order, so that any other
value is still ordered.
*Jean Boussier*
2021-12-17 18:38:50 +08:00
* 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.
```ruby
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*
2021-12-15 11:47:40 +08:00
* Add `authenticate_by` when using `has_secure_password` .
`authenticate_by` is intended to replace code like the following, which
returns early when a user with a matching email is not found:
```ruby
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_by` addresses the vulnerability by taking the same amount of
time regardless of whether a user with a matching email is found:
```ruby
User.authenticate_by(email: "...", password: "...")
```
*Jonathan Hefner*
2021-12-07 23:52:30 +08:00
Please check [7-0-stable ](https://github.com/rails/rails/blob/7-0-stable/activerecord/CHANGELOG.md ) for previous changes.