2021-02-10 14:26:40 -05:00
* Add ability to apply `scoping` to `all_queries` .
Some applications may want to use the `scoping` method but previously it only
worked on certain types of queries. This change allows the `scoping` method to apply
to all queries for a model in a block.
```ruby
Post.where(blog_id: post.blog_id).scoping(all_queries: true) do
post.update(title: "a post title") # adds `posts.blog_id = 1` to the query
end
```
*Eileen M. Uchitelle*
2021-02-10 16:37:18 -05:00
* Switch to database adapter return type for `ActiveRecord::Calculations.calculate`
when called with `:average` (aliased as `ActiveRecord::Calculations.average` )
Previously average aggregation returned `BigDecimal` for everything which
supported `to_d` . This was especially weird for floating point numbers.
Database adapters today map database column types to Ruby types more
accurately than 10 years ago. So now we simply pass them on
(except for special cases which are not `Numeric` ).
```ruby
# With the following schema:
create_table "measurements" do |t|
t.float "temperature"
end
# Before:
Measurement.average(:temperature).class
# => BigDecimal
# After:
Measurement.average(:temperature).class
# => Float
```
*Josua Schmid*
2021-02-10 12:59:31 -05:00
* PostgreSQL: handle `timestamp with time zone` columns correctly in `schema.rb` .
Previously they dumped as `t.datetime :column_name` , now they dump as `t.timestamptz :column_name` ,
and are created as `timestamptz` columns when the schema is loaded.
*Alex Ghiculescu*
2021-02-05 16:40:15 -05:00
* Add `ActiveRecord::Base.connection.with_advisory_lock` .
This method allow applications to obtain an exclusive session level advisory lock,
if available, for the duration of the block.
If another session already have the lock, the method will return `false` and the block will
not be executed.
*Rafael Mendonça França*
2021-02-04 16:32:25 -05:00
* Removing trailing whitespace when matching columns in
`ActiveRecord::Sanitization.disallow_raw_sql!` .
*Gannon McGibbon* , *Adrian Hirt*
2021-01-25 10:52:56 -05:00
* Expose a way for applications to set a `primary_abstract_class`
Multiple database applications that use a primary abstract class that is not
named `ApplicationRecord` can now set a specific class to be the `primary_abstract_class` .
```ruby
class PrimaryApplicationRecord
self.primary_abstract_class
end
```
When an application boots it automatically connects to the primary or first database in the
database configuration file. In a multiple database application that then call `connects_to`
needs to know that the default connection is the same as the `ApplicationRecord` connection.
However some applications have a differently named `ApplicationRecord` . This prevents Active
Record from opening duplicate connections to the same database.
*Eileen M. Uchitelle* , *John Crepezzi*
2021-01-21 21:32:55 -05:00
* Support hash config for `structure_dump_flags` and `structure_load_flags` flags
Now that Active Record supports multiple databases configuration
we need a way to pass specific flags for dump/load databases since
the options are not the same for different adapters.
We can use in the original way:
```ruby
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ['--no-defaults', '--skip-add-drop-table']
#or
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = '--no-defaults --skip-add-drop-table'
```
2021-01-23 05:59:39 -05:00
2021-01-21 21:32:55 -05:00
And also use it passing a hash, with one or more keys, where the key
is the adapter
```ruby
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = {
mysql2: ['--no-defaults', '--skip-add-drop-table'],
postgres: '--no-tablespaces'
2021-01-23 05:59:39 -05:00
}
2021-01-21 21:32:55 -05:00
```
*Gustavo Gonzalez*
2021-01-16 12:06:25 -05:00
* Connection specification now passes the "url" key as a configuration for the
adapter if the "url" protocol is "jdbc", "http", or "https". Previously only
urls with the "jdbc" prefix were passed to the Active Record Adapter, others
are assumed to be adapter specification urls.
Fixes #41137 .
*Jonathan Bracy*
2021-01-19 16:12:18 -05:00
* Allow to opt-out of `strict_loading` mode on a per-record base.
This is useful when strict loading is enabled application wide or on a
model level.
```ruby
class User < ApplicationRecord
2021-02-04 07:36:09 -05:00
has_many :bookmarks
2021-01-19 16:12:18 -05:00
has_many :articles, strict_loading: true
end
user = User.first
2021-01-23 05:59:39 -05:00
user.articles # => ActiveRecord::StrictLoadingViolationError
user.bookmarks # => #< ActiveRecord::Associations::CollectionProxy >
2021-01-19 16:12:18 -05:00
2021-01-23 05:59:39 -05:00
user.strict_loading!(true) # => true
user.bookmarks # => ActiveRecord::StrictLoadingViolationError
user.strict_loading!(false) # => false
user.bookmarks # => #< ActiveRecord::Associations::CollectionProxy >
user.articles.strict_loading!(false) # => #< ActiveRecord::Associations::CollectionProxy >
2021-01-19 16:12:18 -05:00
```
*Ayrton De Craene*
2020-11-23 18:32:11 -05:00
* Add `FinderMethods#sole` and `#find_sole_by` to find and assert the
presence of exactly one record.
Used when you need a single row, but also want to assert that there aren't
multiple rows matching the condition; especially for when database
constraints aren't enough or are impractical.
```ruby
Product.where(["price = %?", price]).sole
# => ActiveRecord::RecordNotFound (if no Product with given price)
# => #< Product . . . > (if one Product with given price)
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
2020-12-30 19:02:34 -05:00
user.api_keys.find_sole_by(key: key)
2020-11-23 18:32:11 -05:00
# as above
```
*Asherah Connor*
2020-12-29 07:11:16 -05:00
* Makes `ActiveRecord::AttributeMethods::Query` respect the getter overrides defined in the model.
2020-12-10 10:37:22 -05:00
Before:
```ruby
2020-12-30 00:44:02 -05:00
class User
def admin
false # Overriding the getter to always return false
2020-12-10 10:37:22 -05:00
end
2020-12-30 00:44:02 -05:00
end
2020-12-10 10:37:22 -05:00
2020-12-30 00:44:02 -05:00
user = User.first
user.update(admin: true)
2020-12-10 10:37:22 -05:00
2020-12-30 00:44:02 -05:00
user.admin # false (as expected, due to the getter overwrite)
user.admin? # true (not expected, returned the DB column value)
2020-12-10 10:37:22 -05:00
```
After this commit, `user.admin?` above returns false, as expected.
2020-12-30 00:44:02 -05:00
Fixes #40771 .
*Felipe*
2020-12-18 01:06:16 -05:00
* Allow delegated_type to be specified primary_key and foreign_key.
Since delegated_type assumes that the foreign_key ends with `_id` ,
`singular_id` defined by it does not work when the foreign_key does
not end with `id` . This change fixes it by taking into account
`primary_key` and `foreign_key` in the options.
*Ryota Egusa*
2020-09-17 16:12:58 -04:00
* Expose an `invert_where` method that will invert all scope conditions.
```ruby
class User
scope :active, -> { where(accepted: true, locked: false) }
end
User.active
# ... WHERE `accepted` = 1 AND `locked` = 0
User.active.invert_where
# ... WHERE NOT (`accepted` = 1 AND `locked` = 0)
```
*Kevin Deisz*
allow passing false to :polymorphic option of belongs_to
before this, passing false would raise the following error
because a condition in AR would disregard the option entirely
if false was passed.
ArgumentError: Unknown key: :polymorphic. Valid keys are:
:class_name, :anonymous_class, :primary_key, :foreign_key,
:dependent, :validate, :inverse_of, :strict_loading, :autosave,
:required, :touch, :counter_cache, :optional, :default
2020-12-17 22:38:36 -05:00
* Restore possibility of passing `false` to :polymorphic option of `belongs_to` .
Previously, passing `false` would trigger the option validation logic
to throw an error saying :polymorphic would not be a valid option.
*glaszig*
2020-12-17 14:41:00 -05:00
* Remove deprecated `database` kwarg from `connected_to` .
*Eileen M. Uchitelle* , *John Crepezzi*
2020-12-11 10:52:00 -05:00
* Allow adding nonnamed expression indexes to be revertible.
Fixes #40732 .
Previously, the following code would raise an error, when executed while rolling back,
and the index name should be specified explicitly. Now, the index name is inferred
automatically.
```ruby
add_index(:items, "to_tsvector('english', description)")
```
*fatkodima*
2020-11-24 12:38:49 -05:00
* Only warn about negative enums if a positive form that would cause conflicts exists.
Fixes #39065 .
*Alex Ghiculescu*
2020-11-19 17:20:45 -05:00
* Add option to run `default_scope` on all queries.
Previously, a `default_scope` would only run on select or insert queries. In some cases, like non-Rails tenant sharding solutions, it may be desirable to run `default_scope` on all queries in order to ensure queries are including a foreign key for the shard (ie `blog_id` ).
Now applications can add an option to run on all queries including select, insert, delete, and update by adding an `all_queries` option to the default scope definition.
```ruby
class Article < ApplicationRecord
default_scope -> { where(blog_id: Current.blog.id) }, all_queries: true
end
```
*Eileen M. Uchitelle*
2020-11-26 08:15:18 -05:00
* Add `where.associated` to check for the presence of an association.
```ruby
# Before:
account.users.joins(:contact).where.not(contact_id: nil)
# After:
account.users.where.associated(:contact)
```
Also mirrors `where.missing` .
*Kasper Timm Hansen*
2020-08-07 15:36:22 -04:00
* Allow constructors (`build_association` and `create_association` ) on
`has_one :through` associations.
*Santiago Perez Perret*
2020-04-14 10:33:56 -04:00
2020-12-02 18:37:26 -05:00
Please check [6-1-stable ](https://github.com/rails/rails/blob/6-1-stable/activerecord/CHANGELOG.md ) for previous changes.