diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-07 17:44:23 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-07 21:04:01 +0900 |
commit | 22360534ac922c68fb0a28f584b48bc0f3633221 (patch) | |
tree | 634c8632646c2a4aa2560df46fad9f0a3e184ffb /activerecord/test/models | |
parent | 2e018361c7c51e36d1d98bf770b7456d78dee68b (diff) | |
download | rails-22360534ac922c68fb0a28f584b48bc0f3633221.tar.gz rails-22360534ac922c68fb0a28f584b48bc0f3633221.tar.bz2 rails-22360534ac922c68fb0a28f584b48bc0f3633221.zip |
Fix `relation.create` to avoid leaking scope to initialization block and callbacks
`relation.create` populates scope attributes to new record by `scoping`,
it is necessary to assign the scope attributes to the record and to find
STI subclass from the scope attributes.
But the effect of `scoping` is class global, it was caused undesired
behavior that pollute all class level querying methods in initialization
block and callbacks (`after_initialize`, `before_validation`,
`before_save`, etc), which are user provided code.
To avoid the leaking scope issue, restore the original current scope
before initialization block and callbacks are invoked.
Fixes #9894.
Fixes #17577.
Closes #31526.
Diffstat (limited to 'activerecord/test/models')
-rw-r--r-- | activerecord/test/models/bird.rb | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/activerecord/test/models/bird.rb b/activerecord/test/models/bird.rb index cfefa555b3..c9f6759c7d 100644 --- a/activerecord/test/models/bird.rb +++ b/activerecord/test/models/bird.rb @@ -16,4 +16,9 @@ class Bird < ActiveRecord::Base def cancel_save_callback_method throw(:abort) end + + attr_accessor :total_count + after_initialize do + self.total_count = Bird.count + end end |