Commit Graph

81686 Commits

Author SHA1 Message Date
Alexandre Ruban
563704080c Fix joining through a polymorphic association 2021-10-06 18:04:44 +02:00
Eileen M. Uchitelle
10c0b5939f
Merge pull request #43372 from eileencodes/add-option-to-lazy-load-schema-cache
Add ability to lazily load the schema cache on connection
2021-10-05 15:45:20 -04:00
eileencodes
d38578afbf
Add ability to lazily load the schema cache on connection
This adds a configuration option and code to enable lazy loading of the
schema cache on the connection. If
`config.active_record.lazily_load_schema_cache` is set to `true` then
the Railtie will be completely skipped and the schema cache will be
loaded when the connection is established rather than on boot.

We use this method at GitHub currently in our custom adapter. It enables
us to load schema caches lazily. It also is a solution for schema caches
with multiple databases. The Railtie doesn't know about the other
connections _before_ boot so it can only load the cache for
`ActiveRecord::Base.connection`. Since this loads the cache on
`establish_connection` Rails doesn't need to know anything special about
the connections.

Applications can continue dumping the cache like normal, the change is
only to how the schema cache is loaded. To use the lazy loaded schema
cache a `schema_cache_path` must be set in the database configuration,
otherwise `db/schema_cache.yml` will be used.

Followup questions:

1) Should we deprecate the Railtie?
2) The Railtie does more work than we're doing here because it checks
the version against the current version. I'm not sure we really want
to do this in Rails - we don't do it in ours at GitHub.
2021-10-05 15:25:31 -04:00
John Hawthorn
9320c856e4
Merge pull request #43373 from jhawthorn/fix_queue_classic_ci
Use queue_classic branch which works on postgresql 14
2021-10-05 10:05:47 -07:00
Jonathan Hefner
87c4cdab5b
Merge pull request #43379 from guigs/patch-2
Remove message from ActiveRecord::Rollback example [ci-skip]
2021-10-05 10:42:04 -05:00
Eileen M. Uchitelle
0a8c4dc0e0
Merge pull request #43358 from composerinteralia/automatic-inverse-of-with-scopes
Automatically infer inverse_of with scopes
2021-10-05 10:22:05 -04:00
Guilherme Goettems Schneider
1beb0ff206
Remove message from ActiveRecord::Rollback example
Do not suggest that raising `ActiveRecord::Rollback` exception should have a message.

There's no point of adding a message to `ActiveRecord::Rollback` exception because it will be rescued by transaction block and the message will be lost.
2021-10-05 16:02:28 +02:00
John Hawthorn
e47a49ffdf Use queue_classic branch which works on psql 14
This fixes the failing CI for ActiveJob.

This uses my fork, we can switch back when the following PR is merged:

    https://github.com/QueueClassic/queue_classic/pull/334
2021-10-04 14:38:56 -07:00
Xavier Noria
fe4377098b Restore set_autoload_path triggering before bootstrap
Fixes #43205.
2021-10-04 22:01:23 +02:00
John Hawthorn
766b84dfe1
Merge pull request #43371 from rails/fix_postgresql_14_tests
Specify ORDER BY enumsortorder for postgres enums
2021-10-04 12:03:08 -07:00
John Hawthorn
9b73129c3e Specify ORDER BY enumsortorder for postgres enums
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2021-10-04 10:08:42 -07:00
Daniel Colson
518b9a301f
Automatically infer inverse_of with scopes
Background
---

I recently noticed we had a number of associations in GitHub that would
benefit from having `inverse_of` set, and so I began adding them. I
ended up adding them to virtually every association with a scope, at
which point I wondered whether Rails might be able to automatically find
these inverses for us.

For GitHub, the changes in this commit end up automatically adding
`inverse_of` to 171 of associations that were missing it.

My understanding is that we do not automatically detect `inverse_of` for
associations with scopes because the scopes could exclude the records we
are trying to inverse from. But I think that should only matter if there
is a scope on the inverse side, not on the association itself.

For example:

Scope on has_many
----

```rb
class Post < ActiveRecord::Base
  has_many :comments, -> { visible }
end

class Comment < ActiveRecord::Base
  belongs_to :post

  scope :visible, -> { where(visible: true) }
  scope :hidden, -> { where(visible: false) }
end

post = Post.create!
comment = post.comments.hidden.create!
assert comment.post
```

This code leaves `post.comments` in sort of a weird state, since it
includes a comment that the association would filter out. But that's
true regardless of the changes in this commit.

Regardless of whether the comments association has an inverse,
`comment.post` will return the post. The difference is that when
`inverse_of` is set we use the existing post we have in memory, rather
than loading it again. If there is a downside to having the `inverse_of`
automatically set here I'm not seeing it.

Scope on belongs_to
----

```rb
class Post < ActiveRecord::Base
  has_many :comments

  scope :visible, -> { where(visible: true) }
  scope :hidden, -> { where(visible: false) }
end

class Comment < ActiveRecord::Base
  belongs_to :post, -> { visible }
end

post = Post.hidden.create!
comment = post.comments.create!
assert_nil comment.post
```

This example is a different story. We don't want to automatically infer
the inverse here because that would change the behavior of
`comment.post`. It should return `nil`, since it's scoped to visible
posts while this one is hidden.

This behavior was not well tested, so this commit adds a test to
ensure we haven't changed it.

Changes
---

This commit changes `can_find_inverse_of_automatically` to allow us to
automatically detect `inverse_of` when there is a scope on the
association, but not when there is a scope on the potential inverse
association. (`can_find_inverse_of_automatically` gets called first with
the association's reflection, then if it returns true we attempt to find
the inverse reflection, and finally we call the method again with the
inverse reflection to ensure we can really use it.)

Since this is a breaking change—specifically in places where code may
have relied on a missing `inverse_of` causing fresh copies of a record
to be loaded—we've placed it behind the `automatic_scope_inversing` flag
(whose name was inspired by `has_many_inversing`). It is set to true for
new applications via framework defaults.

Testing
---

In addition to the inverse association tests, this commit also adds some
cases to a few tests related to preloading. They are basically
duplicates of existing tests, but with lower query counts.

Testing this change with GitHub, the bulk of the failing tests were
related to lower query counts. There were additionally 3 places (2 in
tests and one in application code) where we relied on missing
`inverse_of` causing fresh copies of a record to be loaded.

There's still one Rails test that wouldn't pass if we ran the whole
suite with `automatic_scope_inversing = true`. It's related to
`TaggedPost`, which changes the polymorphic type from the base class
`Post` to the subclass `TaggedPost`.

```rb
class TaggedPost < Post
  has_many :taggings, -> { rewhere(taggable_type: "TaggedPost") }, as: :taggable
end
```

Setting the inverse doesn't work because it ends up changing the type
back to `Post`, something like this:

```rb
post = TaggedPost.new
tagging = post.taggings.new
puts tagging.taggable_type
=> TaggedPost

tagging.taggable = post
puts tagging.taggable_type
=> Post
```

I think this is an acceptable change, given that it's a fairly specific
scenario, and is sort of at odds with the way polymorphic associations
are meant to work (they are meant to contain the base class, not the
subclass). If someone is relying on this specific behavior they can
still either keep `automatic_scope_inversing` set to false, or they can
add `inverse_of: false` to the association.

I haven't found any other cases where having the `inverse_of` would
cause problems like this.

Co-authored-by: Chris Bloom <chrisbloom7@gmail.com>
2021-10-04 09:42:04 -04:00
Eugene Kenny
7600f6af1d Remove unnecessary if in ExtendedDeterministicUniquenessValidator 2021-10-03 21:52:16 +01:00
Eugene Kenny
1e250e6ee2 Replace "ActionText" with "Action Text" [ci skip]
http://guides.rubyonrails.org/api_documentation_guidelines.html#wording
2021-10-03 21:48:05 +01:00
Eugene Kenny
f1f86603a9 Fix new method name in DatabaseConfig#config deprecation message 2021-10-03 17:51:01 +01:00
Eugene Kenny
63be8461f0
Merge pull request #43364 from yykamei/remove-unused-instrumentation-action_controller-hooks
Remove unused instrumentation hooks from action_controller
2021-10-03 16:17:06 +01:00
Yutaka Kamei
d4de20e5c2
Remove unused instrumentation hooks from action_controller
Instrumentation hooks for `write_page.action_controller` and
`expire_page.action_controller` seem to be removed
in https://github.com/rails/rails/pull/7833.
So, subscribers for them are no longer necessary.
2021-10-03 21:23:42 +09:00
John Hawthorn
c2b083df91
Merge pull request #43335 from jhawthorn/faster_conditional_callbacks
Avoid instance_exec for controller callbacks
2021-10-01 09:57:05 -07:00
Jonathan Hefner
7b6d308847
Merge pull request #43350 from bodhish/patch-1
Update the documentation for find_by method [ci-skip]
2021-10-01 11:25:06 -05:00
Jonathan Hefner
6dc55c36e8
Merge pull request #43356 from lavalleeale/patch-1
Change "bash" to "shell" in getting started guide [ci-skip]
2021-10-01 11:22:10 -05:00
Bodhish Thomas
b1e8b5ebc7 Update the documentation for find_by method 2021-10-01 21:49:27 +05:30
Alex Lavallee
dda4b24beb
Change "bash" to "shell" in getting started guide
The command can be run in any shell, so shell is a more accurate word
2021-10-01 08:46:11 -07:00
Jonathan Hefner
5446842968
Merge pull request #43352 from lewispb/lb/clarify-try
Clarify try is like public_send in the guides [ci-skip]
2021-10-01 10:31:54 -05:00
Lewis Buckley
0f1cdec9d7
Clarify try calls public methods only 2021-10-01 13:50:07 +01:00
Ryuta Kamizono
2ed58965fd
Merge pull request #43348 from yasulab/patch-1
Fix invalid link to List of Free Programming Books

[ci-skip]
2021-10-01 12:40:35 +09:00
Yohei Yasukawa
ac3d90968f
Fix invalid link to List of Free Programming Books 2021-10-01 12:09:13 +09:00
Jonathan Hefner
e1b77466c2
Merge pull request #43343 from machinehum/nested-attributes-doc-update
NestedAttribute parent validation documentation update [ci-skip]
2021-09-30 21:29:21 -05:00
Brian Hayden
7d9b5d4ac9 Update docs for nested_attributes to reflect default belongs_to behavior
and validating parent model.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2021-09-30 21:14:47 -05:00
Jonathan Hefner
6106a27a89
Merge pull request #43342 from scottbartell/patch-1
Fix typo in ActiveRecord Changelog [ci-skip]
2021-09-30 14:14:43 -05:00
Scott Bartell
5c5990e1a1
Fix typo 2021-09-30 13:00:22 -04:00
John Hawthorn
b3024484ab Avoid instance_exec for controller callbacks
When a proc is passed to a callback (either as a condition or as the
callback itself) it is evaluated using instance_exec on the controller
instance. Calling instance_exec creates a new singleton class for the
object, which then will require new inline method caches.

This commit avoids creating the extra controller singleton classes when
:only or :except are passed to a callback.
2021-09-29 14:31:47 -07:00
Jonathan Hefner
414e8dea59
Merge pull request #43334 from federicoaldunate/add-that-raises-exception-message-encryptor
Update MessageEncryptor guide to show that an exception is raised [ci-skip]
2021-09-29 12:15:59 -05:00
Federico Aldunate
4860d8534b Update MessageEncryptor guide to show that an exception is raised
swap order
2021-09-29 14:06:58 -03:00
Xavier Noria
d59e729fbf Depend on Zeitwerk 2.5.0.beta5 2021-09-29 09:25:40 +02:00
Jonathan Hefner
03bb5c1dc2
Merge pull request #43323 from muraken-ken/hotfix
Fix wrong API document for form_with helper method [ci-skip]
2021-09-28 10:08:22 -05:00
Jonathan Hefner
8e6817f772
Merge pull request #43313 from eikes/patch-1
Improves ActionView FormHelper check_box documentation [ci-skip] 

Co-authored-by: Petrik <petrik@deheus.net>
2021-09-28 09:10:29 -05:00
muraken-ken
0a3b1ebf6a Fix wrong RDoc 2021-09-28 20:33:35 +09:00
Ryuta Kamizono
999c1181b5
Merge pull request #43311 from krishnasingh001/correct-partial-inserts-default-value
Correct the default value of `partial_inserts` in configuring.md from true to false

[ci-skip]
2021-09-28 19:35:56 +09:00
Krishna Singh
3ec2154f01 Add partial_inserts default configuration for Rails 7.0 in guides/source/configuring.md 2021-09-28 15:33:45 +05:30
Eike S
9b36864595 Improves ActionView FormHelper check_box documentation
The ActionView::Helpers::Tags::CheckBox class accepts the `checked` and `include_hidden` options. The documentation for these options is added to the `check_box` methods in the ActionView::Helpers::FormHelper class where they are exposed.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2021-09-28 10:16:04 +02:00
Xavier Noria
415c07b863
Merge pull request #43321 from ghiculescu/patch-5
Add warning about draft PRs to PR template
2021-09-27 22:08:38 +02:00
Xavier Noria
db41e6c1dd
Update .github/pull_request_template.md 2021-09-27 22:08:06 +02:00
Alex Ghiculescu
99977bb7aa
Warning about draft PRs 2021-09-27 15:03:29 -05:00
Rafael França
38d2e6a19a
Merge pull request #43310 from thedayisntgray/favor-inclusive-language-over-dumb
Replace the word dumb with naive for clarity and inclusivity
2021-09-27 15:56:18 -04:00
Ryuta Kamizono
7753e244c5
Merge pull request #43318 from HParker/add-numeric-params-changelog-entry
Add changelog entry for https://github.com/rails/rails/pull/42501

[ci-skip]
2021-09-28 03:39:23 +09:00
HParker
55811579aa Add changelog entry for https://github.com/rails/rails/pull/42501 2021-09-27 11:17:45 -07:00
Ryuta Kamizono
c7b86bc7e0
Merge pull request #43315 from intrip/fix-codespell-error
Fix codespell `hiearchy ==> hierarchy` error.
2021-09-27 22:53:21 +09:00
Jacopo
f51510309c Fix codespell hiearchy ==> hierarchy error. 2021-09-27 15:04:48 +02:00
Landon Gray
5ef4cd9d76 Replace the word dumb with naive for clarity and inclusivity 2021-09-27 00:10:04 -05:00
Xavier Noria
53000f3a2d More example in the docs about STI and autoloading 2021-09-26 22:35:04 +02:00