From 6eae366d0d2e5d5211eeaf955f56bd1dc6836758 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Wed, 15 Jul 2015 14:07:45 -0400 Subject: Deprecate force association reload by passing true This is to simplify the association API, as you can call `reload` on the association proxy or the parent object to get the same result. For collection association, you can call `#reload` on association proxy to force a reload: @user.posts.reload # Instead of @user.posts(true) For singular association, you can call `#reload` on the parent object to clear its association cache then call the association method: @user.reload.profile # Instead of @user.profile(true) Passing a truthy argument to force association to reload will be removed in Rails 5.1. --- .../lib/active_record/associations/collection_association.rb | 8 ++++++++ .../lib/active_record/associations/singular_association.rb | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 6caadb4ce8..87576abd92 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -1,3 +1,5 @@ +require "active_support/deprecation" + module ActiveRecord module Associations # = Active Record Association Collection @@ -28,6 +30,12 @@ module ActiveRecord # Implements the reader method, e.g. foo.items for Foo.has_many :items def reader(force_reload = false) if force_reload + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing an argument to force an association to reload is now + deprecated and will be removed in Rails 5.1. Please call `reload` + on the result collection proxy instead. + MSG + klass.uncached { reload } elsif stale_target? reload diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index bec9505bd2..30c5d72482 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -1,9 +1,17 @@ +require "active_support/deprecation" + module ActiveRecord module Associations class SingularAssociation < Association #:nodoc: # Implements the reader method, e.g. foo.bar for Foo.has_one :bar def reader(force_reload = false) if force_reload && klass + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing an argument to force an association to reload is now + deprecated and will be removed in Rails 5.1. Please call `reload` + on the parent object instead. + MSG + klass.uncached { reload } elsif !loaded? || stale_target? reload -- cgit v1.2.3