When joining or eager loading an association with a parameterized scope,
the scope block will not be called with a `nil` argument. Rather,
`ActiveRecord::Reflection::AssociationReflection#check_eager_loadable!`
will raise an `ArgumentError`.
Closes#43539.
Co-authored-by: jcoleman <james.coleman@getbraintree.com>
This matches the indentation used in generated code, such as code from
`railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt`.
If we access the pool we can ensure that the ActiveRecord::Base pool is
set back at the end. This will set us up to avoid calling
`establish_connection` from any task / database task. Without this we'll
need to set the connection back when we move to a class specifically for
migration connections.
These helpers are called from both test class definition and test execution,
so we're defining them as both class and instance methods on AR::TestCase.
Defining methods on toplevel changes the behavior of all Ruby Objects which of
course includes the test target, and thus that will spoil the meaning of the
whole testing. This is something that testing libraries or helpers should never
do.
These methods are just called from testing methods, so we can just move them
under AR::TestCase as its instance methods.
This fixes the indentation of the example code of the first bullet
point. The "Learn more" link was moved to the bottom of the bullet point,
similar to the other bullet points.
`ActiveRecord::Base::normalizes` declares a normalization for one or
more attributes. The 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 finder methods. This allows a record to be created
and later queried using unnormalized values. For example:
```ruby
class User < ActiveRecord::Base
normalizes :email, with: -> email { email.strip.downcase }
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.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
```
As of https://github.com/rails/rails/pull/46525, the behaviour around
before_committed! callbacks has changed: callbacks are run on every
enrolled record in a transaction, even multiple copies of the same record.
This is a significant change that apps should be able to opt into in order
to avoid unexpected issues.
In #46270 it was reported that there were database errors when running
the tests in an app. I set up a demo app to use parallel testing and was
able to reproduce. The issue was that the `reconstruct_from_schema`
method needs to use the `pool` and not the `connection` because the
database might not exist yet and will raise an error. I don't know how
to test this inside Rails but I verified this behavior in my demo app.