1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixes strict_loading! documentation

- Fixes typo in the changelog for strict_loading.
- Fixes the example in the changelog for strict_loading. The `strict_loading!` on ActiveRecord object does not disable strict loading on the association if set to strict loading. The example spoke otherwise so updated in a manner to make it more clear.
- Add method level dcoumentation as changelog was only way to communicate how strict_loading could be disabled on the record.
This commit is contained in:
Abhay Nikam 2021-01-23 16:29:39 +05:30
parent af9c910db7
commit 4762721d03
2 changed files with 21 additions and 10 deletions

View file

@ -9,7 +9,7 @@
#or #or
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = '--no-defaults --skip-add-drop-table' ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = '--no-defaults --skip-add-drop-table'
``` ```
And also use it passing a hash, with one or more keys, where the key And also use it passing a hash, with one or more keys, where the key
is the adapter is the adapter
@ -17,7 +17,7 @@
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = { ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = {
mysql2: ['--no-defaults', '--skip-add-drop-table'], mysql2: ['--no-defaults', '--skip-add-drop-table'],
postgres: '--no-tablespaces' postgres: '--no-tablespaces'
} }
``` ```
*Gustavo Gonzalez* *Gustavo Gonzalez*
@ -38,17 +38,20 @@
```ruby ```ruby
class User < ApplicationRecord class User < ApplicationRecord
has_many :bookmarks
has_many :articles, strict_loading: true has_many :articles, strict_loading: true
end end
user = User.first user = User.first
user.articles user.articles # => ActiveRecord::StrictLoadingViolationError
# => ActiveRecord::StrictLoadingViolationError user.bookmarks # => #<ActiveRecord::Associations::CollectionProxy>
user = User.first user.strict_loading!(true) # => true
user.stict_loading!(false) user.bookmarks # => ActiveRecord::StrictLoadingViolationError
user.articles
# => #<ActiveRecord::Associations::CollectionProxy> user.strict_loading!(false) # => false
user.bookmarks # => #<ActiveRecord::Associations::CollectionProxy>
user.articles.strict_loading!(false) # => #<ActiveRecord::Associations::CollectionProxy>
``` ```
*Ayrton De Craene* *Ayrton De Craene*

View file

@ -663,9 +663,17 @@ module ActiveRecord
# if the record tries to lazily load an association. # if the record tries to lazily load an association.
# #
# user = User.first # user = User.first
# user.strict_loading! # user.strict_loading! # => true
# user.comments.to_a # user.comments
# => ActiveRecord::StrictLoadingViolationError # => ActiveRecord::StrictLoadingViolationError
#
# strict_loading! accepts a boolean argument to specify whether
# to enable or disable strict loading mode.
#
# user = User.first
# user.strict_loading!(false) # => false
# user.comments
# => #<ActiveRecord::Associations::CollectionProxy>
def strict_loading!(value = true) def strict_loading!(value = true)
@strict_loading = value @strict_loading = value
end end