diff options
author | Yves Senn <yves.senn@gmail.com> | 2016-11-21 17:19:23 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2016-11-22 12:37:42 +0100 |
commit | 0e9957135d9057f91fe30af01d4a70e30ee8a6b3 (patch) | |
tree | 82bdbe113138eb08dfe3ab7e72ef5f353ef8e7ae /activerecord/lib/active_record/associations | |
parent | b89ddd421feed0a8dc45fb6a9a26dde34e3b7b9e (diff) | |
download | rails-0e9957135d9057f91fe30af01d4a70e30ee8a6b3.tar.gz rails-0e9957135d9057f91fe30af01d4a70e30ee8a6b3.tar.bz2 rails-0e9957135d9057f91fe30af01d4a70e30ee8a6b3.zip |
Introduce `reload_<association>` reader for singular associations.
This patch brings back the functionality of passing true to the
association proxy. The behavior was deprecated with #20888 and scheduled
for removal in Rails 5.1.
The deprecation mentioned that instead of `Article.category(true)` one
should use `article#reload.category`. Unfortunately the alternative does
not expose the same behavior as passing true to the reader
did. Specifically reloading the parent record throws unsaved changes and
other caches away. Passing true only affected the association.
This is problematic and there is no easy workaround. I propose to bring
back the old functionality by introducing this new reader method for
singular associations.
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/singular_association.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 7 |
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index bb96202a22..7732b63af6 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -8,7 +8,16 @@ module ActiveRecord::Associations::Builder # :nodoc: def self.define_accessors(model, reflection) super - define_constructors(model.generated_association_methods, reflection.name) if reflection.constructable? + mixin = model.generated_association_methods + name = reflection.name + + define_constructors(mixin, name) if reflection.constructable? + + mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 + def reload_#{name} + association(:#{name}).force_reload_reader + end + CODE end # Defines the (build|create)_association methods for belongs_to or has_one association diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index e386cc0e4c..1953cc6a72 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -30,6 +30,13 @@ module ActiveRecord record end + # Implements the reload reader method, e.g. foo.reload_bar for + # Foo.has_one :bar + def force_reload_reader + klass.uncached { reload } + target + end + private def create_scope |